Pattern matches(String ,CharSequence) method in Java with Examples

Last Updated : 8 Jul, 2026

The matches(String, CharSequence) method of the Pattern class is a static utility method used to check whether an entire input sequence matches a given regular expression. It internally compiles the regular expression, creates a Matcher, and performs the matching operation in a single step.

  • Returns true if the entire input matches; otherwise returns false.
  • Best suited for one-time regex matching.
Java
import java.util.regex.Pattern;

public class Geeks {

    public static void main(String[] args) {

        String regex = "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com";
        String email = "student123@gmail.com";

        boolean result = Pattern.matches(regex, email);

        if (result) {
            System.out.println("Valid email address.");
        } else {
            System.out.println("Invalid email address.");
        }
    }
}

Output
Valid email address.

Explanation: The Pattern.matches() method checks whether the entire email address matches the given regular expression. Since the email follows the required format, it returns true.

Syntax

public static boolean matches(String regex, CharSequence input)

Parameters:

This method accepts two parameters:

  • regex: This parameter represents the expression to be compiled.
  • input: The character sequence to be matched.

Return Value: Returns true if the entire input matches the regular expression; otherwise returns false.

Program 1: Java program to demonstrate Pattern.matches(String, CharSequence) method

Java
import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "(.*)(ee)(.*)?";

        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks";

        // use matches method to check the match
        boolean matcher = Pattern.matches(REGEX, actualString);

        // print values if match found
        if (matcher) {
            System.out.println("match found for Regex.");
        }
        else {
            System.out.println("No match found for Regex.");
        }
    }
}

Output
match found for Regex.

Explanation: The regular expression (.*)(ee)(.*)? checks whether the input string contains "ee". Since "geeksforgeeks" contains "ee", Pattern.matches() returns true.

Program 2:

Java
import java.util.regex.*;

public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "(.*)(welcome)(.*)?";

        // create the string
        // in which you want to search
        String actualString
            = "The indian team wins worldcup";

        // use matches() method to check the match
        boolean matcher = Pattern.matches(REGEX, actualString);

        // print values if match found
        if (matcher) {
            System.out.println("match found for Regex.");
        }
        else {
            System.out.println("No match found for Regex.");
        }
    }
}

Output
No match found for Regex.

Explanation: The regular expression (.*)(welcome)(.*)? checks whether the input string contains "welcome". Since "The indian team wins worldcup" does not contain this word, Pattern.matches() returns false.

Advantages of matches(String, CharSequence) Method

  • Performs regex matching in a single method call.
  • Returns a simple boolean result.
  • Ideal for one-time pattern validation.
  • Eliminates the need to create separate Pattern and Matcher objects manually.
  • Supports all standard Java regular expression features.
Comment