Regex Possessive Quantifiers Tutorial

There are three different groupings of quantifiers: greedy, reluctant, and possessive. This tutorial is all about possessive quantifiers. Possessive quantifiers are perhaps the most rarely used of all the quantifiers. In my Regex Greedy Quantifiers Tutorial we learned that greedy quantifiers instruct the matcher to match as many instances of its quantified token and return the largest possible string value. In my Regex Reluctant Quantifiers Tutorial we learned that reluctant quantifiers instruct the matcher to match each individual instance of a quantified token and return the smallest possible string value. Once again, the difference between possessive, reluctant, and greedy quantifiers are not always obvious. Possessive quantifiers function just like greedy quantifiers only there is just one simple rule to remember - they never backtrack. The only reason that I can possibly think of to use possessive quantifiers is to increase the speed a regex search in certain situations. With the speed of today's computers, I'm not sure it really matters unless you are running a regex on a database with millions of records. However, if you are running the regex against something like a textbox validation on website or application, I would recommend simply using a greedy quantifier. The syntax for possessive quantifiers is the same as greedy quantifiers only you will simply tack a + on to the greedy quantifier to turn in into a possessive quantifier.
     ?+ 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

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 RegexPossessiveQuantifiers
C:\Java>cd RegexPossessiveQuantifiers

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 RegexPossessiveQuantifiers {
    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", "The Mississippi river");
        displayFind("M(i\\w\\w){0,}+i", "The Mississippi river");
        displayFind("M(i\\w\\w)++i", "The Mississippi river");	
        displayFind("M(i\\w\\w){1,}+i", "The Mississippi river");
        displayFind("M(i\\w\\w){3}+i", "The Mississippi river");

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

        displayFind("<xml>.*</xml>", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.*?</xml>", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.*+</xml>", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.*+", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.++</xml>", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.++", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.?+</xml>", "<xml>YES</xml><xml>NO</xml>");
        displayFind("<xml>.?+", "<xml>YES</xml><xml>NO</xml>");


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

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

C:\Java\RegexPossessiveQuantifiers>java RegexPossessiveQuantifiers
See video for explanation

Final thoughts