English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java 정규 표현식의 “\ D” 유닛 문자

서브식/문자열 " \ D 숫자와 비교하지 않습니다。

예제1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main(String args[]) {
      String regex = "\\D";
      String input = "이는 샘플 텍스트" 12 24 56 89 24";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("매치 수: "}+count);
   }
}

출력 결과

일치 수: 24

예제2

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main(String args[]) {
      String regex = "\\D";
      Scanner sc = new Scanner(System.in);
      System.out.println("입력"); 5 입력 문자열: ");
      String input[] = new String[5];
      for(int i=0; i<5; i++) {
         input[i] = sc.nextLine();
      }
      //Pattern 객체 생성
      Pattern p = Pattern.compile(regex);
      System.out.println("비숫자 문자의 수:");-각 문자열에 있는 숫자 문자: ");
      for(int i=0; i<5;i++) {
         //Matcher 객체 생성
         Matcher m = p.matcher(input[i]);
         int count = 0;
         while(m.find()) {
            count++;
         }
         System.out.println("String "+i+: "+count);
      }
   }
}

출력 결과

입력 5 입력 문자열:
sample 1
12 35 36 63
test 243 663
hello
hello how are you *
비숫자 문자의 수:-각 문자열에 있는 숫자 문자:
String 0: 7
String 1: 3
String 2: 6
String 3: 5
String 4: 19