Matcher requireEnd() method in Java with Examples

Last Updated : 16 Jul, 2026

The requireEnd() method of the Matcher class is used to determine whether additional input could change the result of the previous successful match. It is mainly useful when processing streaming or incomplete input, where more characters may arrive later.

  • Commonly used with regular expressions containing end anchors ($) or lookaheads.
  • Useful for incremental or streaming text processing.
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");

        if (matcher.find()) {

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

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

Output
Match Found: Java
requireEnd(): true

Explanation: The regular expression Java$ matches the word "Java" only if it appears at the end of the input. Since the match depends on the end-of-input anchor ($), requireEnd() returns true. If additional characters are appended, the match result may change.

Syntax

public boolean requireEnd()

Parameters: This method does not accept any parameters.

Return Value

  • true if additional input could change the current successful match.
  • false otherwise.

Example: Using End Anchor ($)

Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        // Regex with end-of-line anchor
        Pattern pattern = Pattern.compile("Spring$");

        Matcher matcher = pattern.matcher(
                "Learn Spring");

        if (matcher.find()) {

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

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

Output
Matched: Spring
requireEnd(): true

Explanation: The $ anchor requires Spring to occur at the end of the input. If more text is appended, the match may no longer be valid. Therefore, requireEnd() returns true.

Example: Without End Anchor

Java
import java.util.regex.*;

public class Geeks {

    public static void main(String[] args) {

        // Regex without any anchor
        Pattern pattern = Pattern.compile("Spring");

        Matcher matcher = pattern.matcher(
                "Learn Spring Framework");

        if (matcher.find()) {

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

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

Output
Matched: Spring
requireEnd(): false

Explanation: The pattern "Spring" does not depend on the end of the input. Even if additional text is appended, the current match remains valid. Therefore, requireEnd() returns false.

Advantages of requireEnd() Method

  • Determines whether additional input could affect the current match result.
  • Useful for parsing streaming or incomplete input.
  • Works well with end anchors ($) and lookahead assertions.
  • Helps implement incremental regular expression matching.
  • Improves reliability when processing real-time text or network data.

Note: The requireEnd() method is an advanced regex feature and is primarily used in streaming parsers or applications where input is received in multiple parts. For regular string matching, methods like find(), matches(), and lookingAt() are more commonly used.

Comment