English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher 클래스는 다양한 매칭 작업을 수행하는 엔진을 나타냅니다. 이 클래스는 생성자를 가지고 있지 않으며 java.util.regex.Pattern 클래스의 matches() 메서드를 사용하여 생성할 수 있습니다./객체를 가져옵니다。
Matcher의lookingAt()메서드는 지역의 시작 부분에서 주어진 입력 텍스트와 패턴을 일치시킵니다. 일치하면 이 메서드는 true를 반환하고, 일치하지 않으면 false를 반환합니다. matches() 메서드와 달리, 이 메서드는 전체 지역이 일치해야 true를 반환하지 않습니다.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test { public static void main(String[] args) { String regex = "(*)(+)(*)"; String input = "This is a sample Text, 1234, with numbers in between. " + "\n This is the second line in the text " + "\n This is third line in the text"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Creating a Matcher object Matcher matcher = pattern.matcher(input); //checking for the match if(matcher.lookingAt()) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } }
输出结果
Match found
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class LookingAtExample { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter String1: "); String input1 = sc.nextLine(); System.out.println("Enter String2: "); String input2 = sc.nextLine(); System.out.println("Enter String3: "); String input3 = sc.nextLine(); String input = input1+"\n"+input2+"\n"+input3; System.out.println(input); //정규 표현식을 통해 숫자를 포함하는 단어를 매칭하는 정규 표현식 String regex = ".*\\d+.*"; //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.lookingAt(); if(bool) { System.out.println("Given input contains digit"); } else { System.out.println("Given input does not contain any digit"); } } }
输出结果
Enter String1: sample text2 Enter String2: data Enter String3: sample sample text2 data sample Given input contains digit