Java : MatchResult (regex) with Examples

MatchResult (Java SE 18 & JDK 18) API 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

String group ()

Returns the input subsequence matched by the previous match.

Please see end().

String group (int group)

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

Please see end(int group).

int groupCount ()

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

Please see end(int group).

int start ()

Returns the start index of the match.

Please see end().

int start (int group)

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

Please see end(int group).


Related posts

To top of page