Regex Greedy Quantifiers Tutorial

There are three different groupings of quantifiers: greedy, reluctant, and possessive. This tutorial is all about greedy quantifiers. What is difference between greedy, reluctant, and possessive quantifiers? The answer is found by studying how the regex engine processes the search string. By default, quantifiers are considered to be greedy. Before I go any further, let me quickly go over the six different types of of greedy quantifiers.
     ? once or not at all
     * zero or more times
     + one or more times
     {x} exactly x times
     {x,} at least x times
     {x,y} at least x but not more than y times

Here are some simple examples:
     colou?r · will match color or colour
     colou*r · will match color or colour
     colou{0,}r · will match color or colour
     M(i\\w\\w)*i · will match Mississippi
     M(i\\w\\w)+i · will match Mississippi
     M(i\\w\\w){3}i · will match Mississippi
     M(i\\w\\w){0,}i · will match Mississippi
     M(i\\w\\w){1,}i · will match Mississippi

That Greedy Word

A greedy quantifier instructs the matcher to match as many instances of its quantified token or sub-pattern as possible. In other words, a greedy quantifier will return back the largest possible string match from the search string. I could spend a bunch of time explaining how the regex engine reads characters in various different ways, but I'll save all that for the reluctant and possessive tutorials. I am just going to give you some simple examples along with an explanation of the results in the video tutorial.



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>
C:\Java>md RegexGreedyQuantifiers
C:\Java>cd RegexGreedyQuantifiers
C:\Java\RegexGreedyQuantifiers>Notepad RegexGreedyQuantifiers.java

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 RegexGreedyQuantifiers {
    public static void main(String args[]) {
        displayFind("colou?r", "Is that a coloring book?");
        displayFind("colou?r", "Is that a colouring book?");
        displayFind("colou*r", "Is that a coloring book?");
        displayFind("colou*r", "Is that a colouring book?");
        displayFind("colou+r", "Is that a coloring book?");
        displayFind("colou+r", "Is that a colouring book?");
        displayFind("colou{0,}r", "Is that a coloring book?");
        displayFind("colou{0,}r", "Is that a colouring book?");
        displayFind("M(i\\w\\w)*i", "Mississippi river");
        displayFind("M(i\\w\\w){0,}i", "Mississippi river");
        displayFind("M(i\\w\\w)+i", "Mississippi river");	
        displayFind("M(i\\w\\w){1,}i", "Mississippi river");
        displayFind("M(i\\w\\w){3}i", "Mississippi river");
        System.out.println();

        displayFind("yoyos.*", "Red Yoyos - I like red yoyos!");
        displayFind(".*yoyos", "Red Yoyos - I like red yoyos!");
        displayFind("yoyos", "Red Yoyos, blue yoyos, green yoyos!");
        System.out.println();

        displayFind("happy.*dogs", "I like happy large dogs and happy small dogs!");

    }

    static void displayFind(String regex, String searchMe) {
        boolean foundIt = false;
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(searchMe);
        while(m.find()){
            System.out.println("Regex " + regex + " found " + m.group() +  " in \"" + searchMe +"\"" );
            foundIt = true;
        }
        if(!foundIt){
            System.out.println("No matches found for " + regex + " in string \"" + searchMe +"\"");
        }  
    }
}

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


C:\Java\RegexGreedyQuantifiers>javac RegexGreedyQuantifiers.java
C:\Java\RegexGreedyQuantifiers>java RegexGreedyQuantifiers
See video for explanation


Final thoughts

Stay tuned to my next tutorial where I will demonstrate reluctant quantifiers and show you how they behave differently from greedy ones.


Tutorials