java.util.regex.Matcher 클래스는 다양한 일치 작업을 수행하는 엔진을 나타냅니다. 이 클래스에 대한 생성자가 없습니다. java.util.regex.Pattern 클래스의 matching() 메소드를 사용하여 이 클래스의 객체를 생성/얻을 수 있습니다.
hitEnd() 메서드는 이전 일치 중에 입력 데이터의 끝에 도달했는지 여부를 확인합니다. 그렇다면 true else false를 반환합니다. 이 메서드가 true를 반환하면 더 많은 입력 데이터가 일치 결과를 변경할 수 있음을 나타냅니다.
예를 들어, 정규식 "you$"를 사용하여 입력 문자열의 마지막 단어를 일치시키려고 하고 첫 번째 입력 줄이 "hello how are you"인 경우 일치할 수 있지만 더 많은 문장을 수락하면 새 줄의 마지막 단어는 필수 단어("당신")가 아닐 수 있으므로 일치 결과가 거짓입니다. 이러한 경우 hitEnd() 메서드는 true를 반환합니다.
예
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HitEndExample {
public static void main( String args[] ) {
String regex = "you$";
//Reading input from user
Scanner sc = new Scanner(System.in);
System.out.println("Enter input text: ");
String input = sc.nextLine();
//Instantiating the Pattern class
Pattern pattern = Pattern.compile(regex);
//Instantiating the Matcher class
Matcher matcher = pattern.matcher(input);
//verifying whether a match occurred
if(matcher.find()) {
System.out.println("Match found");
}
boolean result = matcher.hitEnd();
if(result) {
System.out.println("More input may turn the result of the match false");
} else {
System.out.println("The result of the match will be true, inspite of more data");
}
}
} 출력
Enter input text: Hello how are you Match found More input may turn the result of the match false