例を使用したJavaのMatcherhasAnchoringBounds()メソッド
java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクターはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。
アンカー境界は、^や$などの領域の一致に一致するために使用されます。デフォルトでは、マッチャーはアンカー境界を使用しますが、useAnchoringBounds()メソッドを使用して、アンカー境界の使用から非アンカー境界に切り替えることができます。
hasAnchoringBounds() この(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
-
例を使用したJavaのMatchermatches()メソッド
java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクタはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。 matchs() このクラスのメソッドは、正規表現で表されるパターンと文字列を照合します(両方ともこのオブジェクトの作成中に指定されます)。一致する場合、このメソッドはtrueを返し、そうでない場合はfalseを返します。この方法の結果が真であるためには、領域全体が一致している必要があります。 例 import
-
例を使用したJavaのマッチャーstart()メソッド
java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクタはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。 start() Matcherクラスのメソッドは、一致した文字の開始インデックスを返します。 例 部分式[...]は、入力文字列の中括弧内に指定された文字と一致します。次の例では、これを使用して文字tと一致します。ここで compile()メソッドを使用して正規表現をコンパイルしました。 Ma