Java : MatchResult (regex) with Examples

MatchResult (Java SE 22 & JDK 22) with Examples.
You will find code examples on most MatchResult methods.


Summary

The result of a match operation.

Class diagram

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 ()

Returns the offset after the last character matched.

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)

Returns the offset after the last character of the subsequence captured by the given group during this match.

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)

Returns the offset after the last character of the subsequence captured by the given named-capturing group during the previous match operation.

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 ()

Returns the input subsequence matched by the previous match.

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)

Returns the input subsequence captured by the given group during the previous match operation.

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)

Returns the input subsequence captured by the given named-capturing group during the previous match operation.

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 ()

Returns the number of capturing groups in this match result's pattern.

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 ()

Returns whether this contains a valid match from a previous match or find operation.

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 ()

Returns an unmodifiable map from capturing group names to group numbers.

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 ()

Returns the start index of the match.

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)

Returns the start index of the subsequence captured by the given group during this match.

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)

Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

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

To top of page