java.util.regex.Matcher 클래스는 다양한 일치 작업을 수행하는 엔진을 나타냅니다. 이 클래스에 대한 생성자가 없습니다. java.util.regex.Pattern 클래스의 match() 메소드를 사용하여 이 클래스의 객체를 생성/얻을 수 있습니다.
앵커링 경계는 ^ 및 $와 같은 지역 일치를 일치시키는 데 사용됩니다. 기본적으로 매처는 앵커링 범위를 사용합니다.
useAnchoringBounds() 이 클래스 메소드의 메소드는 부울 값을 허용하며, 이 메소드에 true를 전달하면 현재 매처는 앵커링 범위를 사용하고 false를 전달하면 비앵커링 범위를 사용합니다.
예시 1
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");
}
}
} 출력
Enter input string sample Compiled regular expression: .*\d+.* Current matcher uses non-anchoring bounds
예시 2
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Sample {
public static void main( String args[] ) {
String regex = "^<foo>.*";
String input = "<foo><bar>";//Hi</i></br> welcome to Tutorialspoint";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
matcher = matcher.useAnchoringBounds(false);
if(matcher.matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
System.out.println("Has anchoring bounds: "+matcher.hasAnchoringBounds());
}
} 출력
Match found Has anchoring bounds: false