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 way of checking if String matches to a regular expression in Java or not. Quite often we need to write code which 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 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. 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 example tutorial.

String matches example Java

Java String match example using regular expression
In this String matches tutorial we will see two examples of matches function of String class :

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

In following example of matching String using regular expression we have used two regular expression meta characters 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

Though you can use Pattern and Matcher class for regular expression in Java. matches method of String is 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 iceberg in terms of regular expression but it does show how to use match method of String in Java.

Other String tutorials in Java

No comments:

Post a Comment