Regular Expressions in Java

Regular expressions are used to check if the text contains specific pattern (Regular Expression).

There are 2 most important classes that support regular expressions in Java.

  1. Pattern
  2. Matcher
package regular_expressions;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ssalunke on 12/04/2016.
 */
public class TestRegularExpressions {
    public static void main(String args [] ){
        String sampleText = "My name is Sagar. I live in Brisbane." +
                "My mobile number is 9850182384 and " +
                "email address is [email protected] " +
                "In brisbane there are lots of Parks. I have a Car " +
                "And it's registration number is QLD - 2839" ;

        //Check if above string has Brisbane in it...No matter which case
        Pattern p = Pattern.compile("brisbane",Pattern.CASE_INSENSITIVE);
        Matcher matcher = p.matcher(sampleText);
        if (matcher.find())
        {
            System.out.println("Brisbane is found in the sampleText");
        }else{
            System.out.println("Brisbane is not found in the sampleText");
        }

        //To find how many times, pattern (Brisbane) is found
        // and also print position where the match is found
        matcher.reset();
        int i=0;
        while (matcher.find()){
            i++;
            System.out.println("Match no " + i + " started at "
                    + matcher.start() + " and ended at " + matcher.end()
                    + " What found ? -> " + matcher.group());

        }
        System.out.println("Pattern " + p.toString() + " was found " + i
                + " times in sampleText" );

        //To see if there is a number with 10 digits in sampleText and print that
        matcher.reset();
        p = Pattern.compile("\\d{10}");
        matcher.usePattern(p);
        i=0;
        while (matcher.find()){
            i++;
            System.out.println("Match no " + i + " started at "
                    + matcher.start() + " and ended at " + matcher.end()
                    + " What found ? -> " + matcher.group());

        }
        System.out.println("Pattern " + p.toString() +
                " was found " + i + " times in sampleText" );

        //To see if there is a registration number in sampleText and print that
        matcher.reset();
        p = Pattern.compile("(QLD)( - \\d{4})");
        matcher.usePattern(p);
        i=0;
        while (matcher.find()){
            i++;
            System.out.println("Match no " + i + " started at "
                    + matcher.start() + " and ended at " + matcher.end()
                    + " What found ? -> " + matcher.group());
            System.out.println("Group count -> " + matcher.groupCount() + " "
                    + matcher.group(1) +  matcher.group(2));

        }
        System.out.println("Pattern " + p.toString() +
                " was found " + i + " times in sampleText" );

        //to see if the pattern matches with entire sampleText
        matcher.reset();
        p = Pattern.compile(".*Parks.*");
        matcher.usePattern(p);
        if (matcher.matches()){
            System.out.println("Pattern" + p.toString()
                    +" matches entire sampleText");
        }else{
            System.out.println("Pattern" + p.toString()
                    +" does not match entire sampleText");
        }

        //replacing matched pattern with something else.
        // Here Brisbane will be replaced by Sydney
        p = Pattern.compile("brisbane",Pattern.CASE_INSENSITIVE);
        matcher = p.matcher(sampleText);
        System.out.printf(matcher.replaceAll("Sydney"));
        //to replace first match, use replaceFirst method
    }
}

Here is the output of the above program.

Brisbane is found in the sampleText
Match no 1 started at 28 and ended at 36 What found ? -> Brisbane
Match no 2 started at 115 and ended at 123 What found ? -> brisbane
Pattern brisbane was found 2 times in sampleText
Match no 1 started at 57 and ended at 67 What found ? -> 9850182384
Pattern \d{10} was found 1 times in sampleText
Match no 1 started at 194 and ended at 204 What found ? -> QLD – 2839
Group count -> 2 QLD – 2839
Pattern (QLD)( – \d{4}) was found 1 times in sampleText
Pattern.*Parks.* matches entire sampleText
My name is Sagar. I live in Sydney.My mobile number is 9850182384 and email address is [email protected] In Sydney there are lots of Parks. I have a Car And it’s registration number is QLD – 2839

You may also like...