Regex String Literals Tutorial

In this tutorial I will discuss how string literals are the most simplistic form of pattern matching. The tutorial builds on concepts learned from my Introduction To Regex - Regular Expressions tutorial.

Pattern         Matcher                                     .find() result
lime            "5 limes for $1"                            true · one find
java            "Java Tutorials"                            false · 0 finds - case sensitive
round           "The wheels on the bus go round and round"  true · 2 finds
" "             "The wheels on the bus go round and round"  true · 8 finds
53.254          "107.1.53.254"                              true · 1 finds

.               "107.1.53.254"                              true · 12 finds - say what???
$               "5 limes for $1"                            true · one find
c:\             "c:\java\"                                  PatternSyntaxException

Three of the examples above have patterns that are metacharacters, metacharacters are special characters that affect the outcome of a matching pattern. My next tutorial will teach you what characters are metacharacters and how to use the special characters as part of a regular string literal search.



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 RegexStringLiterals
C:\Java>cd RegexStringLiterals
C:\Java\RegexStringLiterals>Notepad RegexStringLiterals.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 RegexStringLiterals {
    public static void main(String args[]) {
        displayFind("lime","5 limes for $1");
        displayFind("java","Java Tutorials");
        displayFind("round","The wheels on the bus go round and round");
        displayFind(" ","The wheels on the bus go round and round");
        displayFind("53.254","107.1.53.254");
        displayFind(".","107.1.53.254");
        //displayFind("c:\\","c:\\java\\");
    }

    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 RegexStringLiterals.java and press Enter.
Now type in java RegexStringLiterals and press Enter.


C:\Java\RegexStringLiterals>javac RegexStringLiterals.java
C:\Java\RegexStringLiterals>java RegexStringLiterals
Matcher found lime at index 2 for regex lime in string "5 limes for $1"

No matches found for java in string "Java Tutorials'

Matcher found round at index 25 for regex round in string "The wheels on the bus go round and round"
Matcher found round at index 35 for regex round in string "The wheels on the bus go round and round"

Matcher found   at index 3 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 10 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 13 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 17 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 21 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 24 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 30 for regex   in string "The wheels on the bus go round and round"
Matcher found   at index 34 for regex   in string "The wheels on the bus go round and round"

Matcher found 53.254 at index 6 for regex 53.254 in string "107.1.53.254"

Matcher found 1 at index 0 for regex . in string "107.1.53.254"
Matcher found 0 at index 1 for regex . in string "107.1.53.254"
Matcher found 7 at index 2 for regex . in string "107.1.53.254"
Matcher found . at index 3 for regex . in string "107.1.53.254"
Matcher found 1 at index 4 for regex . in string "107.1.53.254"
Matcher found . at index 5 for regex . in string "107.1.53.254"
Matcher found 5 at index 6 for regex . in string "107.1.53.254"
Matcher found 3 at index 7 for regex . in string "107.1.53.254"
Matcher found . at index 8 for regex . in string "107.1.53.254"
Matcher found 2 at index 9 for regex . in string "107.1.53.254"
Matcher found 5 at index 10 for regex . in string "107.1.53.254"
Matcher found 4 at index 11 for regex . in string "107.1.53.254"

...PatternSyntaxException



Final thoughts

Pretty basic stuff on regex string literals, stay tuned to my next tutorial where I will demonstrate regex metacharacters.


Tutorials