java.util.regex Java 패키지는 문자 시퀀스에서 특정 패턴을 찾기 위한 다양한 클래스를 제공합니다.
이 패키지의 패턴 클래스는 정규 표현식의 컴파일된 표현입니다. 따옴표() 이 클래스의 메소드는 문자열 값을 받아들이고 주어진 문자열과 일치하는 패턴 문자열을 반환합니다. 즉, 주어진 문자열에 추가 메타 문자와 이스케이프 시퀀스가 추가됩니다. 어쨌든 주어진 문자열의 의미는 영향을 받지 않습니다.
예시 1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteExample {
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();
System.out.print("Enter the string to be searched: ");
String regex = Pattern.quote(sc.nextLine());
System.out.println("pattern string: "+regex);
//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 found");
} else {
System.out.println("Match not found");
}
}
} 출력
Enter input string This is an example program demonstrating the quote() method Enter the string to be searched: the pattern string: \Qthe\E Match found
예시 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuoteExample {
public static void main( String args[] ) {
String regex = "[aeiou]";
String input = "Hello how are you welcome to Tutorialspoint";
//Compiling the regular expression
Pattern.compile(regex);
regex = Pattern.quote(regex);
System.out.println("pattern string: "+regex);
//Compiling the regular expression
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if(matcher.find()) {
System.out.println("The input string contains vowels");
} else {
System.out.println("The input string does not contain vowels");
}
}
} 출력
pattern string: \Q[aeiou]\E The input string contains vowels