Regex Group Naming Tutorial

In my Regex Capturing Group Numbering Tutorial I demonstrated how capturing groups are automatically numbered when the regex is compiled.

"lizard[^s]|(lizard[s])" .groupCount() = 1
"(lizard[^s])|(lizard[s])" .groupCount() = 2
"((lizard[^s])|(lizard[s]))" .groupCount() = 3
"(?i)((lizard[^s])|(lizard[s]))" .groupCount() = 3

One thing that I pointed out in that tutorial is that if we change the regex at some time in the future, then we run the risk of breaking code that is hard coded to the numbered capturing group. We can prevent that sort of thing from happening by assigning a name to each group. We do this by prefixing our named group with a ?<name>.

"lizard[^s]|(?<plural>lizard[s])" .groupCount() = 1

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

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 RegexGroupNaming {
    public static void main(String args[]) {

        Matcher m = Pattern.compile("(?i)((?<singular>lizard[^s])|(?<plural>lizard[s]))").matcher("Is Godzilla a lizard? Lizards are reptiles, but lizards are just a subclass of reptiles. I think the real question is who really cares?");

        int singular=0, plural=0;
        while(m.find()) {
            System.out.println("ordinary = " +;
            System.out.println(" = " +;
            System.out.println("\"singular\") = " +"singular"));
            System.out.println(" = " +;
            System.out.println("\"plural\") = " +"plural"));

            if ("singular")!=null) { singular++; }
            if ("plural")!=null) { plural++; }
        System.out.println("\nSingular lizard usage: "+singular);
        System.out.println("Plural lizards usage: "+plural);

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

C:\Java\RegexGroupNaming>java RegexGroupNaming
see video

Final thoughts

Stay tuned for my next tutorial where I will teach you about backreferences.