패턴 java.util.regex 클래스 패키지는 정규 표현식의 컴파일된 표현입니다.
toString() 이 클래스의 메소드는 현재 패턴이 컴파일된 정규 표현식의 문자열 표현을 반환합니다.
예시 1
import java.util.Scanner; import java.util.regex.Pattern; public class Example { 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 digits String regex = "(\\d)"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Printing the regular expression System.out.println("Compiled regular expression: "+pattern.toString()); //Verifying whether match occurred if(pattern.matcher(input).find()) System.out.println("Given String contains digits"); else System.out.println("Given String does not contain digits"); } }
출력
Enter input string This 7est contain5 di9its in place of certain charac7er5 Compiled regular expression: (\d) Given String contains digits
예시 2
import java.util.regex.Pattern; public class Example { public static void main(String args[]) { String regex = "Tutorialspoint$"; String input = "Hi how are you welcome to Tutorialspoint"; Pattern pattern = Pattern.compile(regex); Matcher match = pattern.matcher(input); int count = 0; if(match.find()) System.out.println("Match found"); else System.out.println("Match not found"); System.out.println("regular expression: "+pattern.toString()); } }
출력
Match found regular expression: Tutorialspoint$