Pattern Class (Regex) Tutorial

The Pattern class is the primary Java class for compiling regular expressions (regex). In this tutorial I will demonstrate the methods from the Pattern class. If you have been watching my regex tutorial series thus far, then you should already be somewhat familiar with the .compile(String regex) method. The Pattern class and the Matcher class work in conjunction to perform many regex operations.
Consider the following code:
  Pattern p = Pattern.compile("the");
In the statement above, the reference variable p refers to a compiled Pattern object with state that represents the regex the. Regular expressions are by default case sensitive, so how can we create a compiled regex Pattern that will ingore case?
  Pattern p = Pattern.compile("(the)|(THE)|(The)|(THe)|(tHe)|(tHE)|(thE)");
The regex above is crazy, so fortunetly we have a super easy way to create a case-insensitive regex Pattern object. The .compile(String regex, int flags) method is overloaded so we can pass flags to change the behavior of the compiled regex Pattern object.
  Pattern p = Pattern.compile("the", Pattern.CASE_INSENSITIVE);
Now if we invoke the .matcher("The squeaky wheel gets the grease.") method on that string literal the .find() method will return two results.

  Pattern p = Pattern.compile("the", Pattern.CASE_INSENSITIVE);
  Matcher m = p.matcher("The squeaky wheel gets the grease.");
  System.out.println(m.find()); //true
  System.out.println(m.find()); //true
  System.out.println(m.find()); //false

The regex language is very flexible and there is a way to compile the same regex Pattern object using just pure regex and following will do the trick:
  Pattern p = Pattern.compile("(?i)(the)");
However, sometimes it is just easier to read and understand the underlying pattern by using the parameter flags versus regex flags. Don't get me wrong, both ways are equally important to understand and you should strive learn each way. Let's get started.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>md PatternClass
C:\Java>cd PatternClass

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.

import java.util.regex.*;

class PatternClass {
    public static void main(String args[]) {

        System.out.println("Search string: The squeaky wheel gets the grease.");
        Matcher m = Pattern.compile("the", Pattern.CASE_INSENSITIVE).matcher("The squeaky wheel gets the grease.");
        while(m.find()) {
            System.out.println("Pattern.compile(\"the\", Pattern.CASE_INSENSITIVE) index = "+ m.start()); 

        m = Pattern.compile("(?i)(the)").matcher("The squeaky wheel gets the grease.");
        while(m.find()) {
            System.out.println("Pattern.compile(\"(?i)(the)\") index = "+ m.start()); 

        //static boolean matches(String regex, CharSequence input)
        boolean b = Pattern.matches("the","The squeaky wheel gets the grease.");
        System.out.println("Pattern.matches(\"the\",\"The squeaky wheel gets the grease.\") = " + b);
        b = Pattern.matches("the","the");
        System.out.println("Pattern.matches(\"the\",\"the\") = " + b);
        b = Pattern.matches("the","the ");
        System.out.println("Pattern.matches(\"the\",\"the \") = " + b);
        b = Pattern.matches(".the.","The squeaky wheel gets the grease.");
        System.out.println("Pattern.matches(\".the.\",\"The squeaky wheel gets the grease.\") = " + b);
        b = Pattern.matches("the.","the ");
        System.out.println("Pattern.matches(\"the.\",\"the \") = " + b);
        b = Pattern.matches(".{1,}the.{1,}","The squeaky wheel gets the grease.");
        System.out.println("Pattern.matches(\".{1,}the.{1,}\",\"The squeaky wheel gets the grease.\") = " + b);
        // the + quantifier translates into one or more times
        // we combine that with the . wildcard and we'll get our desired result .+
        b = Pattern.matches(".+the.+","The squeaky wheel gets the grease.");
        System.out.println("Pattern.matches(\".+the.+\",\"The squeaky wheel gets the grease.\") = " + b);

        // String pattern()
        Pattern p = Pattern.compile(".{1,}the.{1,}");
        System.out.println("p.pattern() = " + p.pattern());

        // static String quote(String s)
        System.out.println("Pattern.quote(\".{1,}the.{1,}\"); = " + Pattern.quote(".{1,}the.{1,}"));
        System.out.println("Pattern.quote(\"20{4}\"); = " + Pattern.quote("20{4}"));
        System.out.println("Pattern.quote(\"2\\d{4}\"); = " + Pattern.quote("2\\d{4}"));

        // String[] split(CharSequence input) 
        p = Pattern.compile("the", Pattern.CASE_INSENSITIVE);
        String sArray[]	= p.split("The squeaky wheel gets the grease.");
        System.out.println("p = Pattern.compile(\"the\", Pattern.CASE_INSENSITIVE);");
        System.out.println("String sArray[] = p.split(\"The squeaky wheel gets the grease.\")");
        System.out.println("sArray.length = " + sArray.length);
        for(String s: sArray) {
            System.out.println("sArray contains:"+s);
        for(String s: sArray) {

        // int flags()
        p =  Pattern.compile("the", Pattern.CASE_INSENSITIVE);
        System.out.println("p.flags() = "+ p.flags());
        System.out.println("Pattern.CASE_INSENSITIVE = " + Pattern.CASE_INSENSITIVE);

Now switch back to the command prompt (CMD) and type in javac and press Enter.
Now type in java PatternClass and press Enter.

C:\Java\PatternClass>java PatternClass
see video for results

Final thoughts

Now that I have gone over the methods from the Pattern class, stay tuned for my next tutorial where I will discuss some common methods from the Matcher class.