Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

How to use String.matches() with Regular Expression in Java? Example Tutorial

String Matching Example in Java
The string matches method in Java can be used to test String against regular expressions in Java. The string matches() method is one of the most convenient ways of checking if a String matches a regular expression in Java or not. Quite often we need to write code that needs to check if String is numeric, Does String contains alphabets e.g. A to Z or a to Z, How to check if String contains a particular digit 6 times, etc. There are so many cases in Java programming where we need a regular expression. 

If you are familiar with Java's regular expression API then you must know about java.util.regex.Pattern and java.util.regex.Matcher class, this class provides regular expression capability to Java API.

The matches method of the String class actually, delegates the request to these classes. Anyway, the regular expression concept can be best learned by seeing a few examples and this is what we are going to do in this Java String matches the example tutorial.

String matches Example Java

In this String matches tutorial, we will see two examples of matching the function of the String class :

1. How to check if a string contains any alphabetic characters or not in both e.g. A-Z or a-Z
2. How to check if a string contains any numeric digits e.g. digits from 1-9 or 0-9

In the following example of matching String using a regular expression, we have used two regular expression metacharacters like dot (.) which matches any character, and astrick or star (*) which matches any number of times. 

By using .* we are effectively matching any character coming any number of times in the source String in Java. You can further see these Regular Expression courses to learn more about special metacharacters in the regular expression. 

Java String Matches Example - Regular Expression Tutorial

And, here is our complete Java program to demonstrate how to use the matches() method of the String class in Java. 

 * Java program to demonstrate how to use String matches method
 * to match the regular expression in String.
 * @author Javin

public class StringMatchExample {

    public static void main(String args[]) {
        String[] alphabets = {"", "12345", "A12345", "12345B",
                                  "12345a" , "abcd" , "aa343"};
        for(String alphabet : alphabets) {
           System.out.println(" does " + alphabet +
             " contains alphabetic words : " + alphabet.matches(".*[A-Za-z].*"));

        //checking if String contains digits or not
        String[] numbers = {"1234" , "+1234", "234a"};
        for(String number : numbers) {
           System.out.println(" number " + number + " contains only 1-9 digits : "
               + number.matches(".*[1-9].*"));

does  contains alphabetic words: false
does 12345 contains alphabetic words: false
does A12345 contains alphabetic words: true
does 12345B contains alphabetic words: true
does 12345a contains alphabetic words: true
does abcd contains alphabetic words: true
does aa343 contains alphabetic words: true
number 1234 contains only 1-9 digits: true
number +1234 contains only 1-9 digits : true
number 234a contains only 1-9 digits: true

Though you can use Pattern and Matcher class for a regular expression in Java. The matches method of String is a nice shortcut and you can quickly check if a particular string matches a regular expression or not. These String match examples are just the tip of the iceberg in terms of regular expression but it does show how to use the match method of String in Java.

Other String tutorials in Java


  1. The Result: "number 234a contains only 1-9 digits : true"
    Is incorrect. The regexp should be like ".[1-9]*" (if +- signs presence are OK), or "[1-9]*" if only and strictly digits are checked for.

    1. or better it should be treu according to the regex, but it shouldn't say "contains only numbers"


Feel free to comment, ask questions if you have any doubt.