java.util.regex.MatcheResult 인터페이스는 일치 결과를 검색하는 메소드를 제공합니다.
toMatchResult()를 사용하여 이 인터페이스의 개체를 가져올 수 있습니다. Matcher 클래스의 메소드 이 메서드는 현재 matcher의 일치 상태를 나타내는 MatchResult 개체를 반환합니다.
시작() 이 인터페이스의 메소드는 현재 일치의 시작 인덱스를 반환합니다.
예
import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StartExample {
public static void main(String args[]) {
//Reading String from user
System.out.println("Enter a String");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String regex = "\\W";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Match occurred");
}else {
System.out.println("Match not occurred");
}
//Retrieving the MatchResult object
MatchResult res = matcher.toMatchResult();
int start = res.start();
System.out.println(start);
}
} 출력
Enter a String This * is # sample % text with & non word characters Match occurred 4