The end() method of the Matcher class returns the index immediately after the last character of the most recent match. It is commonly used to determine where a matched substring ends in the input string.
- Must be called after a successful find() or matches() operation.
- Returns an integer representing the position after the last matched character.
- Throws IllegalStateException if no successful match has been performed.
import java.util.regex.*;
public class Main {
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: " + matcher.end());
}
}
}
Explanation: The word "Java" starts at index 6 and ends at index 9. Therefore, end() returns 10, which is the position immediately after the last matched character.
Syntax
public int end()
Returns
- An integer representing the index immediately after the last matched character.
Exception
- IllegalStateException –> Thrown if no match has been attempted or the previous match failed.
Example 1: Java code to illustrate end() method
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(G*s)";
// Create a pattern from regex
Pattern pattern
= Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "GeeksForGeeks";
// Create a matcher for the input String
Matcher matcher
= pattern
.matcher(stringToBeMatched);
// Get the current matcher state
MatchResult result
= matcher.toMatchResult();
System.out.println("Current Matcher: "
+ result);
while (matcher.find()) {
// Get the last index of match result
System.out.println(matcher.end());
}
}
}
Output
Current Matcher: java.util.regex.Matcher$ImmutableMatchResult@1dbd16a6 5 13
Explanation: The pattern (G*s) matches the character s at the end of "Geeks" and "Geeks" again. The end() method returns 5 and 13, which are the positions immediately after each matched substring.
Example 2:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
// Get the regex to be checked
String regex = "(G*G)";
// Create a pattern from regex
Pattern pattern
= Pattern.compile(regex);
// Get the String to be matched
String stringToBeMatched
= "GFG FGF GFG";
// Create a matcher for the input String
Matcher matcher
= pattern
.matcher(stringToBeMatched);
// Get the current matcher state
MatchResult result
= matcher.toMatchResult();
System.out.println("Current Matcher: "
+ result);
while (matcher.find()) {
// Get the last index of match result
System.out.println(matcher.end());
}
}
}
Output
Current Matcher: java.util.regex.Matcher$ImmutableMatchResult@1dbd16a6 1 3 6 9 11
Explanation: The pattern (G*G) matches multiple occurrences of G in the input string. For each match, the end() method returns the index immediately after the matched character, resulting in 1, 3, 6, 9, and 11.
Advantages of Matcher.end() Method
- Finds the end position of a matched substring in the input string.
- Helps extract text after a matched pattern.
- Useful for text parsing and string processing tasks.
- Works with find() to process multiple matches efficiently.
- Supports accurate indexing for highlighting or replacing matched text.
Reference: Oracle Doc