【Java入門】Matcherクラスのreset()メソッドの使い方を実例付きで解説
java.util.regex.Matcher クラスは、正規表現によるさまざまなマッチ操作を実行するエンジンを表すクラスです。このクラスにはコンストラクタが用意されておらず、java.util.regex.Pattern クラスの matcher() メソッドを使うことで、Matcher オブジェクトを生成・取得できます。
Matcher クラスの reset() メソッドを呼び出すと、マッチャーが保持しているすべての状態情報が破棄され、文字シーケンスはデフォルトの状態に戻り、追加位置(append position)もゼロにリセットされます。つまり、同じ Matcher オブジェクトを最初からやり直して再利用できるようになるのです。
サンプルコード1:引数なしのreset()メソッド
まずは、引数なしの reset() メソッドの基本的な使い方を見てみましょう。次の例では、HTML内の <b> タグで囲まれた文字列を正規表現で抽出し、その後にマッチャーをリセットしています。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reset {
public static void main(String[] args) {
String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>.";
// bタグの中身にマッチする正規表現
String regex = "<b>(\\S+)</b>";
// Patternオブジェクトの生成
Pattern pattern = Pattern.compile(regex);
// 文字列に対してコンパイル済みパターンでマッチング
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("マッチャーの状態: " + matcher.toMatchResult());
String result = matcher.group(1);
}
// マッチャーをリセット
matcher = matcher.reset();
System.out.println("リセット後のマッチャーの状態: " + matcher.toMatchResult());
}
}実行結果
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>word</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=]
実行結果を見ると、find() を繰り返し呼び出すたびに lastmatch の情報が更新されていきますが、reset() を呼び出した後は lastmatch の情報が空になり、マッチャーの内部状態が初期化されていることが確認できます。
サンプルコード2:文字列を指定してリセットするreset(CharSequence)メソッド
reset() メソッドには、新しい文字列(CharSequence)を引数として受け取るオーバーロード版もあります。このバリアントを使用すると、既存の Matcher オブジェクトをそのまま使い回して、別の文字列に対してマッチングを行うことができます。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Reset {
public static void main(String[] args) {
String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>.";
// bタグの中身にマッチする正規表現
String regex = "<b>(\\S+)</b>";
// Patternオブジェクトの生成
Pattern pattern = Pattern.compile(regex);
// 文字列に対してコンパイル済みパターンでマッチング
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println("マッチャーの状態: " + matcher.toMatchResult());
String result = matcher.group(1);
}
// 新しい文字列でマッチャーをリセット
matcher = matcher.reset("<b>this</b> is <b>new</b> string <b>after</b> reset");
while (matcher.find()) {
System.out.println("リセット後のマッチャーの状態: " + matcher.toMatchResult());
}
}
}実行結果
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>word</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>this</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>new</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>after</b>]
このように、引数付きの reset(CharSequence) を呼び出すと、対象の文字列が新しいものに置き換わり、region の範囲も新しく設定された文字列の長さ(この例では 0〜51)に更新されます。その後の find() 呼び出しでは、新しい文字列に対してマッチングが行われていることがわかります。
まとめ
reset()メソッドは、マッチャーの内部状態(lastmatch 情報など)をすべて破棄し、検索位置を先頭に戻します。reset(CharSequence)メソッドを使うと、新しい文字列を対象としてマッチャーをリセットできます。- これらのメソッドを活用すれば、Pattern の再コンパイルや Matcher の再生成を行うことなく、同一オブジェクトを効率的に再利用できます。
-
Java MatcherクラスのreplaceAll()メソッドの使い方と実例解説
java.util.regex.Matcher クラスは、正規表現によるさまざまなマッチング操作を実行するエンジンを表すクラスです。このクラスにはコンストラクタが用意されておらず、java.util.regex.Pattern クラスの matcher() メソッドを使ってオブジェクトを取得します。Matcher クラスが持つ replaceAll() メソッドは、文字列を引数として受け取り、入力文字列内で正規表現にマッチしたすべての部分文字列を、指定した文字列に置き換えた結果を返します。replaceAll()メソッドの基本構文public String replaceAll(String
-
例で学ぶJava Matcherクラスのpattern()メソッド
java.util.regex.Matcher クラスは、正規表現による各種マッチ操作を実行するエンジンを表すクラスです。このクラスにはコンストラクタが用意されていないため、java.util.regex.Pattern クラスの matcher() メソッドを使用してオブジェクトを生成・取得します。 pattern() メソッドは、現在の Matcher が解釈に使用している Pattern オブジェクトを取得して返します。引数は不要で、戻り値は java.util.regex.Pattern 型です。「実際にどの正規表現で照合を行ったのか」を後から確認したい場合などに便利なメソッドです。