例を使用したJavaのMatcherhasTransparentBounds()メソッド
java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクターはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。
正規表現では、lookbehindおよびlookahead構造は、他のパターンの前または後の特定のパターンに一致するために使用されます。たとえば、5〜12文字を受け入れる文字列を受け入れる必要がある場合、正規表現は-
になります。"\\A(?=\\w{6,10}\\z)"; デフォルトでは、マッチャー領域の境界は、先読み、後読み、および境界マッチングの構成に対して透過的ではありません。つまり、これらの構成は、領域の境界を超えて入力テキストの内容と一致することはできません-
hasTransparentBounds() このクラスメソッドのメソッドは、現在のマッチャーが透過境界を使用しているかどうかを確認します。使用している場合はtrueを返し、そうでない場合はfalseを返します。
例1
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
public static void main(String[] args) {
//Regular expression to accepts 6 to 10 characters
String regex = "\\A(?=\\w{6,10}\\z)";
System.out.println("Enter 5 to 12 characters: ");
String input = new Scanner(System.in).next();
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting region to the input string
matcher.region(0, 4);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
boolean bool = matcher.hasTransparentBounds();
//Switching to transparent bounds
if(bool) {
System.out.println("Current matcher uses transparent bounds");
} else {
System.out.println("Current matcher user non-transparent bound");
}
}
} 出力
Enter 5 to 12 characters: sampletext Match not found Current matcher user non-transparent bound
例2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HasTransparentBounds {
public static void main(String[] args) {
//Regular expression to accepts 6 to 10 characters
String regex = "\\A(?=\\w{6,10}\\z)";
System.out.println("Enter 5 to 12 characters: ");
String input = new Scanner(System.in).next();
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting region to the input string
matcher.region(0, 4);
matcher.useTransparentBounds(true);
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
boolean bool = matcher.hasTransparentBounds();
//Switching to transparent bounds
if(bool) {
System.out.println("Current matcher uses transparent bounds");
} else {
System.out.println("Current matcher user non-transparent bound");
}
}
} 出力
Enter 5 to 12 characters: sampletext Match found Current matcher uses transparent bounds
-
例を使用した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