Java : Matcher (正規表現) - API使用例
Matcher (Java SE 22 & JDK 22) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
API仕様書のおともにどうぞ。
概要
Matcherは、正規表現によるパターンマッチングを行うクラスです。
マッチ操作には、
- 完全一致 ( matches )
- 前方一致 ( lookingAt )
- 部分一致 ( find )
- 置換 ( replaceAll )
などがあります。
マッチ結果として、文字列のどの部分がマッチしたのか?を取得できます。
次のコード例では、正規表現による部分一致を行います。
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");
// 最初に一致するのはどこか?
System.out.println(matcher.find()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 3
System.out.println(matcher.group()); // "abc"
// 次に一致するのはどこか?
System.out.println(matcher.find()); // true
System.out.println(matcher.start()); // 8
System.out.println(matcher.end()); // 13
System.out.println(matcher.group()); // "aaabc"
// もう一致するものがないので false を返します。
System.out.println(matcher.find()); // false
もしくは、MatchResultとして結果を取得することもできます。
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");
final var stream = matcher.results();
stream.forEach(matchResult -> {
System.out.println("------");
System.out.println("start : " + matchResult.start());
System.out.println("end : " + matchResult.end());
System.out.println("group : " + matchResult.group());
});
// 結果
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 13
//group : aaabc
個人的には、後発 API でもある後者の手法をおすすめします。
関連記事:正規表現の基本
メソッド
Matcher appendReplacement (StringBuffer sb, String replacement)
final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");
final var sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "dog");
}
System.out.println(sb); // "one dog two dog"
matcher.appendTail(sb);
System.out.println(sb); // "one dog two dogs in the yard"
Matcher appendReplacement (StringBuilder sb, String replacement)
final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");
final var sb = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(sb, "dog");
}
System.out.println(sb); // "one dog two dog"
matcher.appendTail(sb);
System.out.println(sb); // "one dog two dogs in the yard"
StringBuffer appendTail (StringBuffer sb)
final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");
final var sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "dog");
}
System.out.println(sb); // "one dog two dog"
matcher.appendTail(sb);
System.out.println(sb); // "one dog two dogs in the yard"
StringBuilder appendTail (StringBuilder sb)
final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");
final var sb = new StringBuilder();
while (matcher.find()) {
matcher.appendReplacement(sb, "dog");
}
System.out.println(sb); // "one dog two dog"
matcher.appendTail(sb);
System.out.println(sb); // "one dog two dogs in the yard"
int end ()
final var pattern = Pattern.compile("a+bc");
final var input = "aaaaabc";
final var matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // true
final var start = matcher.start();
System.out.println(start); // 0
final var end = matcher.end();
System.out.println(end); // 7
final var group = input.substring(start, end);
System.out.println(group); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var input = "012abcXYZ";
final var matcher = pattern.matcher(input);
System.out.println(matcher.find()); // true
final var start = matcher.start();
System.out.println(start); // 3
final var end = matcher.end();
System.out.println(end); // 6
final var group = input.substring(start, end);
System.out.println(group); // "abc"
int end (int group)
final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"
final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2
for (int i = 1; i <= groupCount; i++) {
System.out.println("------");
System.out.println("i : " + i);
System.out.println("start : " + matcher.start(i));
System.out.println("end : " + matcher.end(i));
System.out.println("group : " + matcher.group(i));
}
// 結果
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
int end (String name)
final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matcher.start(name)); // 0
System.out.println(matcher.end(name)); // 3
System.out.println(matcher.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matcher.start(name)); // 8
System.out.println(matcher.end(name)); // 11
System.out.println(matcher.group(name)); // "xyz"
}
boolean find ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");
System.out.println(matcher.hasMatch()); // false
{
System.out.println(matcher.find()); // true
System.out.println(matcher.hasMatch()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
}
{
System.out.println(matcher.find()); // true
System.out.println(matcher.hasMatch()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 8
System.out.println(result.end()); // 13
System.out.println(result.group()); // "aaabc"
}
System.out.println(matcher.find()); // false
System.out.println(matcher.hasMatch()); // false
boolean find (int start)
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012 abc xyz aaabc");
{
System.out.println(matcher.find(8)); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 12
System.out.println(result.end()); // 17
System.out.println(result.group()); // "aaabc"
}
{
System.out.println(matcher.find(1)); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 4
System.out.println(result.end()); // 7
System.out.println(result.group()); // "abc"
}
String group ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc xyz");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaabc"
System.out.println(matcher.find()); // false
String group (int group)
final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"
final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2
for (int i = 1; i <= groupCount; i++) {
System.out.println("------");
System.out.println("i : " + i);
System.out.println("start : " + matcher.start(i));
System.out.println("end : " + matcher.end(i));
System.out.println("group : " + matcher.group(i));
}
// 結果
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
String group (String name)
final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matcher.start(name)); // 0
System.out.println(matcher.end(name)); // 3
System.out.println(matcher.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matcher.start(name)); // 8
System.out.println(matcher.end(name)); // 11
System.out.println(matcher.group(name)); // "xyz"
}
int groupCount ()
final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"
final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2
for (int i = 1; i <= groupCount; i++) {
System.out.println("------");
System.out.println("i : " + i);
System.out.println("start : " + matcher.start(i));
System.out.println("end : " + matcher.end(i));
System.out.println("group : " + matcher.group(i));
}
// 結果
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
boolean hasAnchoringBounds ()
final var pattern = Pattern.compile("^abc xyz$");
final var input = "012 abc xyz";
{
final var matcher = pattern.matcher(input);
System.out.println(matcher.hasAnchoringBounds()); // true
// マッチ範囲を "abc xyz" にします。
// regionで指定した範囲として ^ や $ が有効になります。
System.out.println(input.substring(4, 11)); // "abc xyz"
matcher.region(4, 11);
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 4
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc xyz"
}
{
final var matcher = pattern.matcher(input);
matcher.useAnchoringBounds(false);
System.out.println(matcher.hasAnchoringBounds()); // false
matcher.region(4, 11);
System.out.println(matcher.find()); // false
}
boolean hasMatch ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");
System.out.println(matcher.hasMatch()); // false
{
System.out.println(matcher.find()); // true
System.out.println(matcher.hasMatch()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
}
{
System.out.println(matcher.find()); // true
System.out.println(matcher.hasMatch()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 8
System.out.println(result.end()); // 13
System.out.println(result.group()); // "aaabc"
}
System.out.println(matcher.find()); // false
System.out.println(matcher.hasMatch()); // false
boolean hasTransparentBounds ()
final var pattern = Pattern.compile("\\by...");
final var input = "xyz12 yz34";
{
final var matcher = pattern.matcher(input);
System.out.println(matcher.hasTransparentBounds()); // false
// マッチ範囲を "yz12 yz34" にします。
System.out.println(input.substring(1, 10)); // "yz12 yz34"
matcher.region(1, 10);
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "yz12"
}
{
final var matcher = pattern.matcher(input);
matcher.useTransparentBounds(true);
System.out.println(matcher.hasTransparentBounds()); // true
matcher.region(1, 10);
// \b の単語境界として、regionの範囲外も考慮されます。
// つまり、"xyz12" が単語として認識されるため、
// 最初の "yz12" はヒットせず、"yz34" がヒットします。
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "yz34"
}
boolean hitEnd ()
final var pattern = Pattern.compile("cat");
final var matcher = pattern.matcher("one cat two cats in the yard");
while (matcher.find()) {
System.out.println("-------");
System.out.println("hitEnd : " + matcher.hitEnd());
final var result = matcher.toMatchResult();
System.out.println("start : " + result.start());
System.out.println("end : " + result.end());
}
// 結果
// ↓
//-------
//hitEnd : false
//start : 4
//end : 7
//-------
//hitEnd : false
//start : 12
//end : 15
System.out.println(matcher.hitEnd()); // true
boolean lookingAt ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaabc");
System.out.println(matcher.lookingAt()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 5
System.out.println(result.group()); // "aaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcXYZ");
System.out.println(matcher.lookingAt()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");
System.out.println(matcher.lookingAt()); // false
boolean matches ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaaaabc");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 7
System.out.println(result.group()); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("012abc");
System.out.println(matcher.matches()); // false
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abcZ");
System.out.println(matcher.matches()); // false
Map<String,Integer> namedGroups ()
final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
System.out.println("-- groups --");
for (final var entry : matcher.namedGroups().entrySet()) {
System.out.println("entry : " + entry);
final var num = entry.getValue();
System.out.println(" group : " + matcher.group(num));
}
// 結果
// ↓
//true
//-- groups --
//entry : A=1
// group : abc
//entry : B=2
// group : xyz
Pattern pattern ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aaabc");
System.out.println(matcher.pattern()); // "a+bc"
static String quoteReplacement (String s)
final var pattern = Pattern.compile(" ");
final var matcher = pattern.matcher("abc 012 xyz");
final var replacement = Matcher.quoteReplacement("$");
System.out.println(replacement); // "\$"
final var ret = matcher.replaceAll(replacement);
System.out.println(ret); // "abc$012$xyz"
Matcher region (int start, int end)
final var pattern = Pattern.compile("a+bc+");
final var input = "aaabccc";
final var matcher = pattern.matcher(input);
System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);
System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// regionを指定しない例です。
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"
int regionEnd ()
final var pattern = Pattern.compile("a+bc+");
final var input = "aaabccc";
final var matcher = pattern.matcher(input);
System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);
System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// regionを指定しない例です。
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"
int regionStart ()
final var pattern = Pattern.compile("a+bc+");
final var input = "aaabccc";
final var matcher = pattern.matcher(input);
System.out.println(input.substring(2, 6)); // "abcc"
matcher.region(2, 6);
System.out.println(matcher.regionStart()); // 2
System.out.println(matcher.regionEnd()); // 6
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "abcc"
// regionを指定しない例です。
final var pattern = Pattern.compile("a+bc+");
final var matcher = pattern.matcher("aaabccc");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "aaabccc"
String replaceAll (String replacement)
final var pattern = Pattern.compile("a*b");
final var matcher = pattern.matcher("aabfooaabfooabfoob");
final var result = matcher.replaceAll("-");
System.out.println(result); // "-foo-foo-foo-"
String replaceAll (Function<MatchResult,String> replacer)
final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");
final var result = matcher.replaceAll(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzDOGzzz"
String replaceFirst (String replacement)
final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");
final var result = matcher.replaceFirst("cat");
System.out.println(result); // "zzzcatzzzdogzzz"
String replaceFirst (Function<MatchResult,String> replacer)
final var pattern = Pattern.compile("dog");
final var matcher = pattern.matcher("zzzdogzzzdogzzz");
final var result = matcher.replaceFirst(mr -> mr.group().toUpperCase());
System.out.println(result); // "zzzDOGzzzdogzzz"
boolean requireEnd ()
final var pattern = Pattern.compile("abc$");
final var matcher = pattern.matcher("abc");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // true
final var pattern = Pattern.compile("abc");
final var matcher = pattern.matcher("abcd");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.requireEnd()); // false
Matcher reset ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"
System.out.println(matcher.find()); // false
matcher.reset();
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"
System.out.println(matcher.find()); // false
Matcher reset (CharSequence input)
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "abc"
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"
System.out.println(matcher.find()); // false
matcher.reset("aaaabc");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aaaabc"
System.out.println(matcher.find()); // false
Stream<MatchResult> results ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aabc");
final var stream = matcher.results();
stream.forEach(matchResult -> {
System.out.println(matchResult.group());
});
// 結果
// ↓
// "abc"
// "aabc"
int start ()
final var pattern = Pattern.compile("a+bc");
final var input = "aaaaabc";
final var matcher = pattern.matcher(input);
System.out.println(matcher.matches()); // true
final var start = matcher.start();
System.out.println(start); // 0
final var end = matcher.end();
System.out.println(end); // 7
final var group = input.substring(start, end);
System.out.println(group); // "aaaaabc"
final var pattern = Pattern.compile("a+bc");
final var input = "012abcXYZ";
final var matcher = pattern.matcher(input);
System.out.println(matcher.find()); // true
final var start = matcher.start();
System.out.println(start); // 3
final var end = matcher.end();
System.out.println(end); // 6
final var group = input.substring(start, end);
System.out.println(group); // "abc"
int start (int group)
final var pattern = Pattern.compile("(abc|efg)_012_(x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
System.out.println(matcher.start()); // 0
System.out.println(matcher.end()); // 11
System.out.println(matcher.group()); // "abc_012_xyz"
final var groupCount = matcher.groupCount();
System.out.println(groupCount); // 2
for (int i = 1; i <= groupCount; i++) {
System.out.println("------");
System.out.println("i : " + i);
System.out.println("start : " + matcher.start(i));
System.out.println("end : " + matcher.end(i));
System.out.println("group : " + matcher.group(i));
}
// 結果
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
int start (String name)
final var pattern = Pattern.compile("(?<A>abc|efg)_012_(?<B>x..)");
final var matcher = pattern.matcher("abc_012_xyz");
System.out.println(matcher.matches()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matcher.start(name)); // 0
System.out.println(matcher.end(name)); // 3
System.out.println(matcher.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matcher.start(name)); // 8
System.out.println(matcher.end(name)); // 11
System.out.println(matcher.group(name)); // "xyz"
}
MatchResult toMatchResult ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");
{
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 0
System.out.println(result.end()); // 3
System.out.println(result.group()); // "abc"
}
{
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 4
System.out.println(result.end()); // 8
System.out.println(result.group()); // "aabc"
}
String toString ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc aabc");
System.out.println(matcher.find()); // true
final var str = matcher.toString();
// java.util.regex.Matcher[pattern=a+bc region=0,8 lastmatch=abc]
System.out.println(str);
Matcher useAnchoringBounds (boolean b)
final var pattern = Pattern.compile("^abc xyz$");
final var input = "012 abc xyz";
{
final var matcher = pattern.matcher(input);
System.out.println(matcher.hasAnchoringBounds()); // true
// マッチ範囲を "abc xyz" にします。
// regionで指定した範囲として ^ や $ が有効になります。
System.out.println(input.substring(4, 11)); // "abc xyz"
matcher.region(4, 11);
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.start()); // 4
System.out.println(result.end()); // 11
System.out.println(result.group()); // "abc xyz"
}
{
final var matcher = pattern.matcher(input);
matcher.useAnchoringBounds(false);
System.out.println(matcher.hasAnchoringBounds()); // false
matcher.region(4, 11);
System.out.println(matcher.find()); // false
}
Matcher usePattern (Pattern newPattern)
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("aabc xyz");
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "aabc"
System.out.println(matcher.find()); // false
matcher.usePattern(Pattern.compile("x.."));
System.out.println(matcher.find()); // true
System.out.println(matcher.group()); // "xyz"
Matcher useTransparentBounds (boolean b)
final var pattern = Pattern.compile("\\by...");
final var input = "xyz12 yz34";
{
final var matcher = pattern.matcher(input);
System.out.println(matcher.hasTransparentBounds()); // false
// マッチ範囲を "yz12 yz34" にします。
System.out.println(input.substring(1, 10)); // "yz12 yz34"
matcher.region(1, 10);
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "yz12"
}
{
final var matcher = pattern.matcher(input);
matcher.useTransparentBounds(true);
System.out.println(matcher.hasTransparentBounds()); // true
matcher.region(1, 10);
// \b の単語境界として、regionの範囲外も考慮されます。
// つまり、"xyz12" が単語として認識されるため、
// 最初の "yz12" はヒットせず、"yz34" がヒットします。
System.out.println(matcher.find()); // true
final var result = matcher.toMatchResult();
System.out.println(result.group()); // "yz34"
}
関連記事
- API 使用例