English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcherクラスは、さまざまなマッチング操作を実行するエンジンを表します。このクラスにはコンストラクタがなく、以下を使用して使用できます。matches()
java.util.regex.Patternのメソッドで作成/このクラスのオブジェクトを取得します。
正規表現では、lookbehindおよびlookahead構造は、他の特定のパターン之前または後にパターンをマッチングするために使用されます。例えば、あなたが5から12文字の文字列、その場合の正規表現は-
"\\A(?=\\w{6,10}\\z)";
デフォルトでは、マッチングエリアの境界は、前方向、後方向、および境界マッチングの構造に対して不透明であり、これらの構造はエリア境界の外の入力テキスト内容をマッチングできない-
このクラスメソッドのhasTransparentBounds()このメソッドは、現在のマッチャーが透明な境界線を使用しているかどうかを確認し、使用している場合はtrue、そうでない場合はfalseを返します。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HasTransparentBounds { public static void main(String[] args) { //正規表現が受け入れられる6から10文字 String regex = "\\A(?=\\w{6,10}\\z)"; System.out.println("Enter 5 to 12 characters: "); String input = new Scanner(System.in).next(); //パターンオブジェクトを作成します Pattern pattern = Pattern.compile(regex); //Matcher オブジェクトを作成 Matcher matcher = pattern.matcher(input); //エリアを入力文字列に設定 matcher.region(0, 4); if(matcher.find()) { System.out.println("Match found"); } else { System.out.println("Match not found"); } boolean bool = matcher.hasTransparentBounds(); //透明範囲に切り替え 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 マッチングが見つかりませんでした 現在のマッチャーは非-透明な境界線
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HasTransparentBounds { public static void main(String[] args) { //正規表現が受け入れられる6から10文字 String regex = "\\A(?=\\w{6,10}\\z)"; System.out.println("Enter 5 to 12 characters: "); String input = new Scanner(System.in).next(); //パターンオブジェクトを作成します Pattern pattern = Pattern.compile(regex); //Matcher オブジェクトを作成 Matcher matcher = pattern.matcher(input); //エリアを入力文字列に設定 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(); //透明範囲に切り替え 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