例を使用したJavaのマッチャーregionEnd()メソッド
java.util.regex.Matcherクラスは、さまざまな一致操作を実行するエンジンを表します。このクラスのコンストラクターはありません。クラスjava.util.regex.Patternのmatches()メソッドを使用して、このクラスのオブジェクトを作成/取得できます。
regionEnd() この(Matcher)クラスのメソッドは、現在のマッチャーオブジェクトの終了インデックスを表す整数値を返します。
例1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
public static void main(String[] args) {
String regex = "(.*)(\\d+)(.*)";
String input = "This is a sample Text, 1234, with numbers in between.";
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Creating a Matcher object
Matcher matcher = pattern.matcher(input);
//Setting the region of the matcher
matcher.region(5, 20);
if(matcher.matches()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
System.out.print("End of the region: "+matcher.regionEnd());
}
} 出力
Match not found End of the region: 20
例2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegionEndExample {
public static void main(String[] args) {
//Regular expression to accepts 6 to 10 characters
String regex = "[#]";
System.out.println("Enter a string: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
//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(2, 4);
//Switching to transparent bounds
if(matcher.find()) {
System.out.println("Match found");
} else {
System.out.println("Match not found");
}
System.out.println("Ending of the region: "+ matcher.regionEnd());
}
} 出力
Enter a string: this is sample text # Match not found Ending of the region: 4
-
例を使用した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