메타 문자 "." 정규식을 사용하여 모든 문자를 인쇄하려면 모든 문자와 일치 -
-
compile() 메서드를 사용하여 정규식을 컴파일합니다.
-
matcher() 메소드를 사용하여 Matcher 객체를 생성합니다.
-
find() 메서드를 사용하여 일치 항목을 찾고 모든 일치 항목에 대해 group() 메서드를 사용하여 일치하는 내용(문자)을 인쇄합니다.
예
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main( String args[] ) {
//Regular expression to match a string of non-word with length 2 to 6
String regex = ".";
Scanner sc = new Scanner(System.in);
System.out.println("Enter your input string: ");
String input = sc.nextLine();
//Creating a Pattern object
Pattern p = Pattern.compile(regex);
//Creating a Matcher object
Matcher m = p.matcher(input);
while(m.find()) {
System.out.println(m.group());
}
}
} 출력
Enter your input string: This is a sample text T h i s i s a s a m p l e t e x t