Title: Matching RegEx capability
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Matching RegEx capability

The Pattern class contains a lot of the Regular Expression power. It also works in conjuction with another class called the Matcher class. In this set of notes we will just focus on whether or not a text string matches the definition of what is defined in a Regular Expression. In another set of notes we will look at some of the other capabilities.

Again Java API documentation for java.util.regex.Pattern contains a lot of Regular expression definitions.

We will use three methods for matching:

String text;
..... text somehow gets a value
boolean text.matches(String regularExpression);

boolean Pattern.matches(String regularExpression, String text);

// The following would be used if we had one pattern to compare with a lot of text strings.

Pattern pattern = Pattern.compile(String regularExpression);
Matcher matcher = pattern.matcher(String text) ;
boolean matcher.matches();

regular expressions showing in the code below:

Reg Ex as String | Reg Ex read from keyboard | Definition of what the Reg Ex does
"cat" | cat | Just what you think
"\\w+" | \w+ | A sequence of one or more of the following characters: letter: a through z or A through Z digit: 0 through 9
"\\d+" | \d+ | One or more digits: 0 through 9
"[\\.\\d]+" | [\.\d]+ | One or more of the characters in the following set: 0 through 9 or .
"b.y" | b.y | A three character sequence that starts with a 'b' and ends with a 'y'. The middle character can be anything.
"[a-z]\\.\\)" | [a-z]\.\) | A three character sequence that starts with a lower case letter followed by '.' and ends with a ')' letter.
"[\\.\\w]+@[\\.\\w]+"
[\.\w]+@[\.\w]+
Primitive email address definition:
If we define dotted_word as a sequence of one or more of the following: letter, digit, or '.'

This regular expression is:

dotted_word@dotted_word

Example: Clem.Hasselbach2@yahoo.com
"[a-zA-Z][_\\-\\w]*" | [a-zA-Z][_\-\w]* | Variable definition: Starts with at least one letter followed by zero or more of the following: letter, digit, '_', '-'
"males?|females?" | males?|females? | One of the following: male males female females
"\\s*" | \s* | Zero or more white space characters: space, tab, new-line

A few more syntax rules found in the java.util.regex.Pattern documentation:

[abc]	a, b, or c (simple class)
[^abc]	Any character except a, b, or c (negation)
[a-zA-Z]	a through z or A through Z, inclusive (range)
[a-d[m-p]]	a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]]	d, e, or f (intersection)
[a-z&&[^bc]]	a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]]	a through z, and not m through p: [a-lq-z](subtraction)


[aeiou]         vowel
[a-z&&[^aeiou]] consonant

Matching.java

package regex;


import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Matching{

    static void doesItMatch(String regex, String text)
    {
        System.out.println("************  doesItMatch  ******************");
        System.out.println("Regular Expression: "+regex);
        System.out.println("Text: "+text);

        if (text.matches(regex))
            System.out.println("text.matches: true");
        else
            System.out.println("text.matches: false");

        if (Pattern.matches(regex,text))
            System.out.println("Pattern.matches: true");
        else
            System.out.println("Pattern.matches: false");

// Alternative way to do the same thing
// We might use this if we were using the same pattern on many strings.

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher=pattern.matcher(text);
        if (matcher.matches())
            System.out.println("matcher.matches: true");
        else
            System.out.println("matcher.matches: false");

         System.out.println("----------------------------\n\n\n");
    }


    public static void main(String[] args) {

        //Dumb example
        doesItMatch("cat",
                "cat");

        // Word?
        doesItMatch("\\w+",
                "aBc123xyZ");

        // The following won't match
        doesItMatch("\\w+",
                "abc123 xyz");

        //Number
        doesItMatch("\\d+",
                "1234901");

        //Number again
        doesItMatch("[\\.\\d]+",
                "1234.901");

        // Matches boy buy bay
        doesItMatch("b.y",
                "boy");

        // Matches a lower case letter followed by a '.' followed by ')'
        doesItMatch("[a-z]\\.\\)",
                "f.)");

        // Is it an email address? yes
        doesItMatch("[\\.\\w]+@[\\.\\w]+",
                "Joe.smith@yahoo.com");

        // Is it an email address? no
        doesItMatch("[\\.\\w]+@[\\.\\w]+",
                "Joe.smith:yahoo.com");

        //Variable - yes
        doesItMatch("[a-zA-Z][_\\-\\w]*",
                "boundary_condition-3");

        //Variable - no
        doesItMatch("[a-zA-Z][_\\-\\w]*",
                "4boundary_condition-3");

        // gender : matches any of the following: male males female females
        doesItMatch("males?|females?",
                "female");

        // Only white space
        doesItMatch("\\s*",
                "   \t  \t   \n  \t");

    }



}





Last Updated: June 8, 2015 4:36 AM