java.util.regex.Matcher 클래스는 다양한 일치 작업을 수행하는 엔진을 나타냅니다. 이 클래스에 대한 생성자가 없습니다. java.util.regex.Pattern 클래스의 match() 메소드를 사용하여 이 클래스의 객체를 생성/얻을 수 있습니다.
toString() Matcher 클래스의 메소드는 현재 matcher 객체의 내용을 나타내는 문자열 값을 반환합니다.
예시 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ToStringExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "[#%&*]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
int count =0;
while(matcher.find()) {
count++;
}
//Retrieving Pattern used
System.out.println("The are "+count+" special [# % & *] characters in the given text");
System.out.println("Following is the string format of the matcher used: \n"+matcher.toString());
}
} 출력
Enter input text: Hello# How # are# you *& welcome to T#utorials%point The are 7 special [# % & *] characters in the given text Following is the string format of the matcher used: java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]
예시 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ToStringExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
String regex = "[#%&*]";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
int count =0;
while(matcher.find()) {
count++;
}
//Retrieving Pattern used
System.out.println("The are "+count+" special [# % & *] characters in the given text");
System.out.println("Following is the string format of the matcher used: \n"+matcher.toString());
}
} 출력
Enter input text: Hello# How # are# you *& welcome to T#utorials%point The are 7 special [# % & *] characters in the given text Following is the string format of the matcher used: java.util.regex.Matcher[pattern=[#%&*] region=0,52 lastmatch=]