Matcher end(int) method in Java with Examples

Last Updated : 16 Jul, 2026

The end(int group) method of the Matcher class returns the offset immediately after the last character matched by the specified capturing group during the most recent successful match. It is commonly used to determine where a particular captured group ends within the input string.

  • Returns the ending offset (exclusive) of the specified capturing group.
  • Works only after a successful match using methods like find() or matches().
  • Useful for locating the ending position of captured groups in regular expression matching.
Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("(Java)");
        Matcher matcher = pattern.matcher("Learn Java Programming");

        if (matcher.find()) {
            System.out.println("End Index (Exclusive): "
                    + matcher.end(1));
        }
    }
}

Output
End Index (Exclusive): 10

Explanation: The regular expression (Java) creates one capturing group. The word "Java" starts at index 6 and ends at index 9. Since end(int) returns the offset immediately after the last matched character, end(1) returns 10.

Syntax

public int end(int group)

Parameter

  • group: The index of the capturing group whose ending position is required.

Return Value: Returns the offset immediately after the last character matched by the specified capturing group.

Exceptions

  • IllegalStateException: If no successful match has been attempted or the previous match failed.
  • IndexOutOfBoundsException: If the specified capturing group does not exist.

Example: Getting the Ending Offset of a Capturing Group

Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("(G*s)");
        Matcher matcher = pattern.matcher("GeeksForGeeks");

        while (matcher.find()) {
            System.out.println("End Index (Exclusive): "
                    + matcher.end(1));
        }
    }
}

Output
End Index (Exclusive): 5
End Index (Exclusive): 13

Explanation: The regular expression (G*s) matches zero or more 'G' characters followed by 's'. The pattern matches the 's' character in both occurrences of "Geeks". The end(1) method returns the index immediately after each matched group, which is 5 and 13.

Example: Getting the Ending Offset of the Entire Match

Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        Pattern pattern = Pattern.compile("(G*G)");
        Matcher matcher = pattern.matcher("GFG FGF GFG");

        while (matcher.find()) {
            System.out.println("End Index (Exclusive): "
                    + matcher.end(0));
        }
    }
}

Output
End Index (Exclusive): 1
End Index (Exclusive): 3
End Index (Exclusive): 6
End Index (Exclusive): 9
End Index (Exclusive): 11

Explanation: The regular expression (G*G) matches sequences ending with the character 'G'. Here, end(0) returns the exclusive ending index of the entire matched pattern. The matches end at positions 1, 3, 6, 9, and 11, indicating the index immediately after each match.

Difference between end() and end(int)

MethodDescription
end()Returns the exclusive ending index of the entire match.
end(0)Also returns the exclusive ending index of the entire match.
end(int group)Returns the exclusive ending index of the specified capturing group.

Advantages of end(int) Method

  • Returns the exclusive ending position of a specific capturing group.
  • Useful for locating the boundaries of matched groups.
  • Works seamlessly with capturing groups in regular expressions.
  • Helps extract substrings using start and end indices.
  • Simplifies parsing and processing structured text using Java Regex.
Comment