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>
C:\Java>md RegexGroupNaming
C:\Java>cd RegexGroupNaming
C:\Java\RegexGroupNaming>Notepad RegexGroupNaming.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 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?");
        System.out.println(m.groupCount());

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

            if (m.group("singular")!=null) { singular++; }
            if (m.group("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 RegexGroupNaming.java and press Enter.
Now type in java RegexGroupNaming and press Enter.


C:\Java\RegexGroupNaming>javac RegexGroupNaming.java
C:\Java\RegexGroupNaming>java RegexGroupNaming
see video


Final thoughts

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


Tutorials