Regex Character Classes Part 2 Tutorial

In this tutorial we'll build on concepts learned from my Regex Character Classes Part 1 Tutorial. I will be covering unions, intersections, and subtraction in this tutorial.

Unions · nested character classes contained inside square brackets

Pattern         Matcher                             .find() result
[aeiou[0-9]]    "5 limes for $1"                    true · five finds
[aeiou0-9]      "5 limes for $1"                    true · five finds
[a-zA-Z]        "Java Tutorials"                    true · thirteen finds
[a-z[A-Z]]      "Java Tutorials"                    true · thirteen finds

don't use negation in unions
[aeiou[^0-9]]   "5 limes for $1"                    true · twelve finds
[aeiou^0-9]     "59 limes for $10"                  true · seven finds

Intersections · nested character classes contained inside square brackets separated with &&

The intersection "&&" characters causes the .find() method to match only characters common to both character classes. In my opinion intersections are somewhat strange and seem somewhat useless, but I'll demonstrate how they work anyway.

Pattern             Matcher                             .find() result
[aeiou&&[ae]]       "Java Tutorials"                    true · three finds
[ae]                "Java Tutorials"                    true · three finds
[0-9&&[4-6]]        "5 limes for $1"                    true · one find
[0-9&&[6-8]]        "5 limes for $1"                    false · no finds

Subtraction · nested character classes contained inside square brackets separated with && with at least one character class containing the "^" negation metacharacter

Whereas the intersection is quite useless in my opinion, the subtraction character class is quite useful in certain cases. The subtraction character class is identical to the intersection, but it include the negation character in at leat one of the classes.

Pattern                 Matcher                              .find() result
[A-z&&[^aeiou]]         "Java Tutorials"                     true · seven finds - only consonants
[A-z&&[^aeiou]]         "Java_[Tutorials]"                   true · ten finds
[a-z[A-Z]&&[^aeiou]]    "Java_[Tutorials]"                   true · seven finds - only consonants



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 RegexCharactersTwo
C:\Java>cd RegexCharactersTwo
C:\Java\RegexCharactersTwo>Notepad RegexCharactersTwo.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 RegexCharactersTwo {
    public static void main(String args[]) {
        displayFind("[aeiou[0-9]]","5 limes for $1");
        displayFind("[aeiou0-9]","5 limes for $1");
        displayFind("[a-zA-Z]","Java Tutorials");
        displayFind("[a-z[A-Z]]","Java Tutorials");
        displayFind("[aeiou[^0-9]]","5 limes for $1");
        displayFind("[aeiou^0-9]","59 limes for $10");

        displayFind("[aeiou&&[ae]]","Java Tutorials");
        displayFind("[ae]","Java Tutorials");
        displayFind("[0-9&&[4-6]]","5 limes for $1");
        displayFind("[0-9&&[6-8]]","5 limes for $1");

        displayFind("[A-z&&[^aeiou]]","Java Tutorials");
        displayFind("[A-z&&[^aeiou]]","Java_[Tutorials]");
        displayFind("[a-z[A-Z]&&[^aeiou]]","Java_[Tutorials]");
    }

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

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


C:\Java\RegexCharactersTwo>javac RegexCharactersTwo.java
C:\Java\RegexCharactersTwo>java RegexCharactersTwo
Matcher found 5 at index 0 for regex [aeiou[0-9]] in string "5 limes for $1"
Matcher found i at index 3 for regex [aeiou[0-9]] in string "5 limes for $1"
Matcher found e at index 5 for regex [aeiou[0-9]] in string "5 limes for $1"
Matcher found o at index 9 for regex [aeiou[0-9]] in string "5 limes for $1"
Matcher found 1 at index 13 for regex [aeiou[0-9]] in string "5 limes for $1"

Matcher found 5 at index 0 for regex [aeiou0-9] in string "5 limes for $1"
Matcher found i at index 3 for regex [aeiou0-9] in string "5 limes for $1"
Matcher found e at index 5 for regex [aeiou0-9] in string "5 limes for $1"
Matcher found o at index 9 for regex [aeiou0-9] in string "5 limes for $1"
Matcher found 1 at index 13 for regex [aeiou0-9] in string "5 limes for $1"

Matcher found J at index 0 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found a at index 1 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found v at index 2 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found a at index 3 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found T at index 5 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found u at index 6 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found t at index 7 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found o at index 8 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found r at index 9 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found i at index 10 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found a at index 11 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found l at index 12 for regex [a-zA-Z] in string "Java Tutorials"
Matcher found s at index 13 for regex [a-zA-Z] in string "Java Tutorials"

Matcher found J at index 0 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found a at index 1 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found v at index 2 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found a at index 3 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found T at index 5 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found u at index 6 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found t at index 7 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found o at index 8 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found r at index 9 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found i at index 10 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found a at index 11 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found l at index 12 for regex [a-z[A-Z]] in string "Java Tutorials"
Matcher found s at index 13 for regex [a-z[A-Z]] in string "Java Tutorials"

Matcher found   at index 1 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found l at index 2 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found i at index 3 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found m at index 4 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found e at index 5 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found s at index 6 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found   at index 7 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found f at index 8 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found o at index 9 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found r at index 10 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found   at index 11 for regex [aeiou[^0-9]] in string "5 limes for $1"
Matcher found $ at index 12 for regex [aeiou[^0-9]] in string "5 limes for $1"

Matcher found 5 at index 0 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found 9 at index 1 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found i at index 4 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found e at index 6 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found o at index 10 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found 1 at index 14 for regex [aeiou^0-9] in string "59 limes for $10"
Matcher found 0 at index 15 for regex [aeiou^0-9] in string "59 limes for $10"

Matcher found a at index 1 for regex [aeiou&&[ae]] in string "Java Tutorials"
Matcher found a at index 3 for regex [aeiou&&[ae]] in string "Java Tutorials"
Matcher found a at index 11 for regex [aeiou&&[ae]] in string "Java Tutorials"

Matcher found a at index 1 for regex [ae] in string "Java Tutorials"
Matcher found a at index 3 for regex [ae] in string "Java Tutorials"
Matcher found a at index 11 for regex [ae] in string "Java Tutorials"

Matcher found 5 at index 0 for regex [0-9&&[4-6]] in string "5 limes for $1"

No matches found for [0-9&&[6-8]] in string "5 limes for $1'

Matcher found J at index 0 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found v at index 2 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found T at index 5 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found t at index 7 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found r at index 9 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found l at index 12 for regex [A-z&&[^aeiou]] in string "Java Tutorials"
Matcher found s at index 13 for regex [A-z&&[^aeiou]] in string "Java Tutorials"

Matcher found J at index 0 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found v at index 2 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found _ at index 4 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found [ at index 5 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found T at index 6 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found t at index 8 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found r at index 10 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found l at index 13 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found s at index 14 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found ] at index 15 for regex [A-z&&[^aeiou]] in string "Java_[Tutorials]"

Matcher found J at index 0 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found v at index 2 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found T at index 6 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found t at index 8 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found r at index 10 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found l at index 13 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"
Matcher found s at index 14 for regex [a-z[A-Z]&&[^aeiou]] in string "Java_[Tutorials]"



Final thoughts


Tutorials