java.util.regex Java 패키지는 문자 시퀀스에서 특정 패턴을 찾기 위해 다양한 클래스를 제공합니다.
이 패키지의 패턴 클래스는 정규 표현식의 컴파일된 표현입니다. 매처() 이 클래스의 메서드는 CharSequence의 개체를 허용합니다. 입력 문자열을 나타내는 클래스이며, 주어진 문자열을 현재(Pattern) 개체가 나타내는 정규식과 일치시키는 Matcher 개체를 반환합니다.
예
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatcherExample {
public static void main(String args[]) {
//Reading string value
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string");
String input = sc.nextLine();
//Regular expression to find vowels
String regex = "[aeiou]";
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("Given string contains vowels");
} else {
System.out.println("Given string does not contain vowels");
}
}
} 출력
Enter input string RHYTHM Given string does not contain vowels