Matcher hitEnd() method in Java with Examples

Last Updated : 16 Jul, 2026

The hitEnd() method of the Matcher class is used to determine whether the last matching operation reached the end of the input sequence. It is typically used after a matching operation such as find(), matches(), or lookingAt() to check if additional input might be required to complete the match.

  • Commonly used while processing streaming or partial input.
  • Helpful when performing incremental regular expression matching.
Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        String input = "Java Java";

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

        while (matcher.find()) {

            System.out.println("Match: "
                    + matcher.group());

            System.out.println("hitEnd(): "
                    + matcher.hitEnd());
        }

        // After all matches are completed
        System.out.println("After matching: "
                + matcher.hitEnd());
    }
}

Output
Match: Java
hitEnd(): false
Match: Java
hitEnd(): false
After matching: true

Explanation: The pattern "Java" is found twice in the input string. During each successful match, there is still input remaining to search, so hitEnd() returns false. After the final search attempt reaches the end of the input and no more matches are found, hitEnd() returns true.

Syntax

public boolean hitEnd()

Parameters: This method does not accept any parameters.

Return Value

  • true if the previous matching operation reached the end of the input sequence.
  • false otherwise.

Example 1: Check Whether Matching Reached the End

Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        // Regex to search
        Pattern pattern = Pattern.compile("Spring");

        // Input string
        Matcher matcher = pattern.matcher(
                "Spring Boot Spring Framework");

        while (matcher.find()) {

            // Print matched text
            System.out.println("Found: "
                    + matcher.group());

            // Check whether matching reached end
            System.out.println("hitEnd(): "
                    + matcher.hitEnd());
        }

        System.out.println("Final hitEnd(): "
                + matcher.hitEnd());
    }
}

Output
Found: Spring
hitEnd(): false
Found: Spring
hitEnd(): false
Final hitEnd(): true

Explanation

The regex finds Spring twice. During matching there is still text left to search, so hitEnd() returns false. Once the matcher finishes scanning the entire input, hitEnd() becomes true.

Example 2:

Java
import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {

        // Get the regex to be checked
        String regex = "(GFG)";

        // Create a pattern from regex
        Pattern pattern
            = Pattern.compile(regex);

        // Get the String to be matched
        String stringToBeMatched
            = "GFGFGFGFGFGFGFGFGFG FGF GFG GFG FGF";

        // Create a matcher for the input String
        Matcher matcher
            = pattern.matcher(stringToBeMatched);

        while (matcher.find()) {

            System.out.println("Group matched: "
                               + matcher.group());

            // Check if the matching has ended
            // using hitEnd() method
            System.out.println("Has matching hit end: "
                               + matcher.hitEnd());
        }

        // Check if the matching has ended
        // using hitEnd() method
        System.out.println("Has matching hit end: "
                           + matcher.hitEnd());
    }
}

Output
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Group matched: GFG
Has matching hit end: false
Has matching hit end: true
Comment