Matcher matches() method in Java with Examples

Last Updated : 6 Jul, 2026

The matches() method of the Matcher class checks whether the entire input sequence matches the given regular expression. It returns true only if the complete input matches the pattern; otherwise, it returns false.

  • Checks whether the entire input string matches the pattern.
  • Returns a boolean value (true or false).
  • Does not perform partial matching.
Java
import java.util.regex.*;

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

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

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

Output
Matches: true

Explanation: The input string "Java" exactly matches the regular expression "Java". Therefore, matches() returns true.

Syntax

public boolean matches()

  • Parameters: This method does not take any parameters.
  • Returns: A boolean value indicating whether the entire input sequence matches the regular expression.

Example 1: Java code to illustrate matches() method

Java
import java.util.regex.*;

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

        // Get the regex to be checked
        String regex = "Geeks";

        // 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 result state
        // using matches() method
        System.out.println("Result: "
                           + matcher.matches());
    }
}

Output
Result: false

Explanation: The input string "GeeksForGeeks" is longer than the pattern "Geeks". Since matches() requires the entire string to match the pattern, it returns false.

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";

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

        // Get the result state
        // using matches() method
        System.out.println("Result: "
                           + matcher.matches());
    }
}

Output
Result: false

Explanation: The pattern "GFG" matches only a single "GFG", while the input string contains multiple repeated occurrences. Since the entire input does not exactly match the pattern, matches() returns false.

Advantages of Matcher.matches() Method

  • Checks whether the entire input string matches the regular expression.
  • Returns a simple boolean value (true or false) for easy validation.
  • Useful for validating inputs such as email addresses, phone numbers, ZIP codes, and passwords.
  • Easy to use with Pattern and Matcher classes for regex-based matching.
  • Helps ensure exact matching, preventing partial or incorrect matches
Comment