java.util.regex.Matcher 클래스는 다양한 일치 작업을 수행하는 엔진을 나타냅니다. 이 클래스에 대한 생성자가 없습니다. java.util.regex.Pattern 클래스의 match() 메소드를 사용하여 이 클래스의 객체를 생성/얻을 수 있습니다.
앵커링 경계는 ^ 및 $와 같은 지역 일치를 일치시키는 데 사용됩니다. 기본적으로 매처는 앵커링 바운드를 사용하며 useAnchoringBounds() 메서드를 사용하여 앵커링 바운드 사용에서 비앵커링 바운드로 전환할 수 있습니다.
hasAnchoringBounds() 이 (Matcher) 클래스의 메소드는 현재 matcher가 앵커링 경계를 사용하는지 확인하고, 그렇다면 true를 반환하고, 그렇지 않으면 false를 반환합니다.
예시 1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasAnchoringBoundsExample {
public static void main(String[] args) {
String regex = "(.*)(\\d+)(.*)";
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);
//Verifying for anchoring bounds
boolean bool = matcher.hasAnchoringBounds();
//checking for the match
if(bool) {
System.out.println("Current matcher uses anchoring bounds");
} else {
System.out.println("Current matcher uses non-anchoring bounds");
}
if(matcher.matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
}
} 출력
Current matcher uses anchoring bounds Match not found
예시 2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Trail {
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());
//Retrieving the matcher object
Matcher matcher = pattern.matcher(input);
matcher.useAnchoringBounds(false);
boolean hasBounds = matcher.hasAnchoringBounds();
if(hasBounds) {
System.out.println("Current matcher uses anchoring bounds");
} else {
System.out.println("Current matcher uses non-anchoring bounds");
}
//verifying whether match occurred
if(matcher.matches()) {
System.out.println("Given String contains digits");
} else {
System.out.println("Given String does not contain digits");
}
}
} 출력
Enter input string hello sample 2 Compiled regular expression: .*\d+.* Current matcher uses non-anchoring bounds Given String contains digits