Java String Matches Example - Regular Expression Tutorial

String Matching Example in Java
String matches method in Java can be used to test String against regular expression in Java. String matches() method is one of the most convenient ways of checking if 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 than you must know about java.util.regex.Pattern and java.util.regex.Matcher class, these class provides regular expression capability to Java API.

The matches method of String class actually delegate request to these classes. Anyway regular expression can be best learn by seeing 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 matches function of 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 regular expression we have used two regular expression metacharacters e.g. 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 time in source String in Java.

 * Java program to demonstrate how to use use String matches method
 * to match 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
Java String match example using regular expression

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

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Java Fundamentals: The Java Language
Complete Java Masterclass

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.