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

Java에서 "\w" 키워드 표시기를 설명

부분 표현식/기본 문자열 " \ w 와 단어 문자와 일치하며, 즉 a부터 z와 A부터 Z 및 0부터9。

예제1

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
   public static void main( String args[] ) {
      String regex = "\\w to";
      String input = "Hello how are you welcome to w"3codebox";
      Pattern p = Pattern.compile(regex);
      Matcher m = p.matcher(input);
      int count = 0;
      while(m.find()) {
         count++;
      }
      System.out.println("Number of matches: ")+count);
   }
}

출력 결과

일치 횟수: 1

예제2

다음 예제에서 입력합니다5문자열 값을 가져오고 단어 문자를 포함하는 값을 출력합니다-

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchWordCharacters {
   public static void main( String args[] ) {
      String regex = "\\w.*$";
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter 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]);
         if(m.matches()) {
            System.out.println(m.group());
         }
      }
   }
}

출력 결과

입력 5 입력 문자열:
sample
test
test23
hello##
#$%&&
단어 문자를 포함하는 문자열:
sample
test
test23
hello##