Java : MatchResult (regex) with Examples
MatchResult (Java SE 22 & JDK 22) with Examples.
You will find code examples on most MatchResult methods.
Summary
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");
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());
});
// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc
Methods
int end ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");
System.out.println(matcher.lookingAt()); // true
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");
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());
});
// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc
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
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
final var groupCount = matchResult.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 : " + matchResult.start(i));
System.out.println("end : " + matchResult.end(i));
System.out.println("group : " + matchResult.group(i));
}
// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matchResult.start(name)); // 0
System.out.println(matchResult.end(name)); // 3
System.out.println(matchResult.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matchResult.start(name)); // 8
System.out.println(matchResult.end(name)); // 11
System.out.println(matchResult.group(name)); // "xyz"
}
String group ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");
System.out.println(matcher.lookingAt()); // true
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");
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());
});
// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc
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
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
final var groupCount = matchResult.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 : " + matchResult.start(i));
System.out.println("end : " + matchResult.end(i));
System.out.println("group : " + matchResult.group(i));
}
// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matchResult.start(name)); // 0
System.out.println(matchResult.end(name)); // 3
System.out.println(matchResult.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matchResult.start(name)); // 8
System.out.println(matchResult.end(name)); // 11
System.out.println(matchResult.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
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
final var groupCount = matchResult.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 : " + matchResult.start(i));
System.out.println("end : " + matchResult.end(i));
System.out.println("group : " + matchResult.group(i));
}
// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
default boolean hasMatch ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaabc");
{
System.out.println(matcher.find()); // true
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.hasMatch()); // true
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
}
{
System.out.println(matcher.find()); // true
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.hasMatch()); // true
System.out.println(matchResult.start()); // 8
System.out.println(matchResult.end()); // 13
System.out.println(matchResult.group()); // "aaabc"
}
{
System.out.println(matcher.find()); // false
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.hasMatch()); // false
}
default 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
final var matchResult = matcher.toMatchResult();
System.out.println("-- groups --");
for (final var entry : matchResult.namedGroups().entrySet()) {
System.out.println("entry : " + entry);
final var num = entry.getValue();
System.out.println(" group : " + matchResult.group(num));
}
// Result
// ↓
//true
//-- groups --
//entry : A=1
// group : abc
//entry : B=2
// group : xyz
int start ()
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc");
System.out.println(matcher.lookingAt()); // true
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 3
System.out.println(matchResult.group()); // "abc"
final var pattern = Pattern.compile("a+bc");
final var matcher = pattern.matcher("abc 012 aaaabc 345");
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());
});
// Result
// ↓
//------
//start : 0
//end : 3
//group : abc
//------
//start : 8
//end : 14
//group : aaaabc
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
final var matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
final var groupCount = matchResult.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 : " + matchResult.start(i));
System.out.println("end : " + matchResult.end(i));
System.out.println("group : " + matchResult.group(i));
}
// Result
// ↓
//------
//i : 1
//start : 0
//end : 3
//group : abc
//------
//i : 2
//start : 8
//end : 11
//group : xyz
default 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 matchResult = matcher.toMatchResult();
System.out.println(matchResult.start()); // 0
System.out.println(matchResult.end()); // 11
System.out.println(matchResult.group()); // "abc_012_xyz"
{
final var name = "A";
System.out.println(matchResult.start(name)); // 0
System.out.println(matchResult.end(name)); // 3
System.out.println(matchResult.group(name)); // "abc"
}
{
final var name = "B";
System.out.println(matchResult.start(name)); // 8
System.out.println(matchResult.end(name)); // 11
System.out.println(matchResult.group(name)); // "xyz"
}
Related posts
- API Examples