String matches() Method in Java with Examples

Last Updated : 14 Jul, 2026

The matches() method of the String class is used to determine whether a string completely matches a given regular expression (regex). It returns true if the entire string matches the specified pattern; otherwise, it returns false.

  • Returns a boolean value (true or false).
  • Uses Java's Regular Expression (Regex) engine.
  • Commonly used for input validation and pattern matching.
Java
public class Geeks {

    public static void main(String[] args) {

        String str = "12345";

        boolean result = str.matches("\\d+");

        System.out.println(result);
    }
}

Output
true

Explanation: The regular expression \\d+ matches one or more digits (0-9). Since the string "12345" contains only digits, the matches() method returns true.

Syntax

boolean matches(String regex)

Parameter:

  • regex: The regular expression to match against the string.

Return Type:

  • It returns true if the string matches the regex, otherwise false.

Examples of String matches() Method

Check for Alphabets using matches()

We will use matches() method to check if a string contains only alphabetic characters.

Java
import java.util.*;

public class StringMatches {

    public static void main(String[] args) {

        String s = "JavaProgramming";

        // Check if the string contains only alphabets
        boolean r = s.matches("[a-zA-Z]+"); // Matches only alphabets (case-insensitive)

        System.out.println("" + r);
    }
}

Output
true

Explanation: The regular expression [a-zA-Z]+ matches one or more uppercase or lowercase English letters. Since the string "JavaProgramming" contains only alphabetic characters, the matches() method returns true.

Validate an Email Address using matches()

To validate an email addresses, regular expressions can be used with matches() method.

Java
import java.util.*;

public class StringMatches {

    public static void main(String[] args) {

        String email = "example@domain.com";

        // Check if the string is a valid email address
        boolean r = email.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$");

        System.out.println("" + r);
    }
}

Output
true

Explanation: The regular expression checks whether the string follows a valid email format by verifying the username, @ symbol, domain name, and top-level domain. Since "example@domain.com" satisfies this pattern, the matches() method returns true.

Check a String Pattern using matches()

To check whether a string follows a specific pattern like combination of digits or letters, we can use matches() method of String class.

Java
import java.util.*;

public class StringMatches {

    public static void main(String[] args) {

        String s = "Java123";

        // Check if the string contains alphabets followed by digits
        boolean r = s.matches("[A-Za-z]+\\d+");

        System.out.println("" + r);
    }
}

Output
true

Explanation: The regular expression [A-Za-z]+\\d+ matches a string containing one or more letters followed by one or more digits. Since "Java123" follows this format, the matches() method returns true.

Common Regular Expressions Used with matches()

Regular ExpressionDescription
\\d+One or more digits
[a-zA-Z]+One or more alphabets
[A-Za-z]+\\d+Alphabets followed by digits
\\w+Word characters (letters, digits, underscore)
\\s+One or more whitespace characters
.*Any sequence of characters
[6-9]\\d{9}Valid 10-digit mobile number
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$Email validation

Advantages of matches() Method

  • Simple way to validate text using regular expressions.
  • Returns a boolean result directly.
  • Supports complex pattern matching.
  • Useful for validating user input.
  • Eliminates the need for manual string parsing.
  • Works with Java's built-in Regex API.
Comment