java.util.regex.Matcher 클래스는 다양한 일치 작업을 수행하는 엔진을 나타냅니다. 이 클래스에 대한 생성자가 없습니다. java.util.regex.Pattern 클래스의 match() 메소드를 사용하여 이 클래스의 객체를 생성/얻을 수 있습니다.
일치() 이 클래스의 메소드는 정규 표현식으로 표현되는 패턴과 문자열을 일치시킵니다(둘 다 이 객체를 생성하는 동안 제공됨). 일치하는 경우 이 메서드는 true를 반환하고 그렇지 않으면 false를 반환합니다. 이 방법의 결과가 참이 되려면 전체 지역이 일치해야 합니다.
예
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchesExample {
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.next();
//Regular expression to match words that starts with digits
String regex = "^[0-9].*$";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
//verifying whether match occurred
boolean bool = matcher.matches();
if(bool) {
System.out.println("First character is a digit");
} else{
System.out.println("First character is not a digit");
}
}
} 출력
Enter a String 4hiipla First character is a digit