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

SubString RegEx capability

In this section of Notes we are going to focus more on the Pattern and Matcher class capabilities.

Here we will be focusing on regular expressions that define subsets of a String and what operations we want to do.

Matcher methods:

boolean find(); - find the next occurence of the regular expression in the text

String group(); - String found in the last occurence of find

int start(); - starting index where the object was found

int end(); - ending index (non-inclusive) of where the object was found

appendReplacement(StringBuffer, String replacementValue);

appendTail(StringBuffer);

Pattern methods:

String[] split(String text); - Returns an array of Strings left over from the found regular expressions

regular expressions showing in the code below:

Reg Ex as String | Reg Ex read from keyboard | Definition of what the Reg Ex does
"cat|dog" | cat|dog | cat or dog
"[cC]at" | [cC]at | cat or Cat
",\\s*|;\\s*|\\.\\s+|\\s+"
,\s*|;\s*|\.\s+|\s+
The wspace mean white space.

',' followed by zero or more wspace OR
';' followed by zero or more wspace
OR
'.' followed by one or more wspace
OR
one or more wspace
"\\d+" | \d+ | One or more digits
"\\d{3}" | \d{3} | sequences of 3 digits
"\\d{3,4}" | \d{3,4} | digit sequences with either 3 or 4 digits
"\\d{3,}" | \d{3,} | digit sequences with at least 3 digits
"[Ss]cientist\\b" | [Ss]cientist\b | Scientist or scientist with a word boundary on the right. In otherwords scientists won't work
"\\b[Ss]cientists?\\b" | \b[Ss]cientists?\b | Matches Scientist Scientists scientist scientists with a word boundary on the right and the left. In otherwords neither scientistMan or neuroscientist works
"^\\s*\\w+" | ^\s*\w+ | Word at the beginning of the line. Optional leading whitespace is OK.
"\\w+\\s*$" | \w+\s*$ | Word at theend of the line. Optional trailing whitespace is OK.

SubstringMatching.java

package regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SubstringMatching {

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

        // For anything more, we should use the more full blown classes:

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher=pattern.matcher(text);


        while (matcher.find())
        {
            System.out.println("matcher.group()="+matcher.group() +
                    " start="+matcher.start() + " end="+matcher.end());
        }

        System.out.println("*****  -- splitting *******");
        String[] arr = pattern.split(text);
        System.out.println("******************");
        for (int i=0; i < arr.length; i++)
            System.out.println(arr[i]);


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

    }

    static void replaceExample()
    {
        System.out.println("\n\n*******  Replace Example ***********");
        Pattern p = Pattern.compile("cat");
        Matcher m = p.matcher("one cat two cats in the yard full of cats  in a world of cats");
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            m.appendReplacement(sb, "dog");
        }
        m.appendTail(sb);
        System.out.println(sb.toString());
    }


    public static void main(String[] args) {

        process("cat|dog",
                "The cat and the dog have lots of dogs and cats in the cat-dog catalog");

        process("cat",
                "The cat is a big Cat who has lots of cats");

        process("[cC]at",
                "The cat is a big Cat who has lots of cats");



/*   -------------------------
        process("\\s+",
                "wheat    barley corn oats    rice");

        process("[,;\\s\\.]+",
                "wheat.germ    barley, corn; oats.    rice");

        process(",\\s*|;\\s*|\\.\\s+|\\s+",
                "wheat.germ    barley, corn; oats.    rice");

*/


/*   -------------------------
        process("\\d+",
                "  abc1def23ghi456jklmn7890opqr12345stuv678901wxyz");

        process("\\d{3}",
                "  abc1def23ghi456jklmn7890opqr12345stuv678901wxyz");

        process("\\d{3,4}",
                "  abc1def23ghi456jklmn7890opqr12345stuv678901wxyz");

        process("\\d{3,}",
                "  abc1def23ghi456jklmn7890opqr12345stuv678901wxyz");
*/


/*   --------------------------
        process("scientist",
                "Scientist  scientist  neoroscientist scientistperson scientists ");

        // Book end: \b
        process("[Ss]cientist\\b",
                "Scientist  scientist neoroscientist  scientistperson scientists ");

        process("[Ss]cientists?\\b",
                "Scientist  scientist neoroscientist scientistperson scientists ");

        process("\\b[Ss]cientists?\\b",
                "Scientist  scientist neoroscientist scientistperson scientists ");


*/

/*  --------------
        // Finding email addresses
        process("[\\.\\w]+@[\\.\\w]+",
                "chasselb@wccnet.edu; Subject Text cc: John.Smith3@gmail.com some more text @ the countertop");
*/


/*  --------------
        // First Word on the line
        process("^\\s*\\w+"
                ,"    First things : 1 2 3456 last word on the line is Last   ");

        // Last Word on the line
        process("\\w+\\s*$"
                ,"    First things : 1 2 3456 last word on the line is Last   ");

*/

/*  --------------
        // This is the underlying logic to the String replaceAll feature
        replaceExample();
*/
    }



}



This is example won't be on any video because this topic is beyond what I want to cover. I have it here as a reference of an example that uses groups.



import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GroupExample {

    public static void main(String[] args) {

         System.out.println("\n\n*********  Group Example *************");
         Pattern pattern = Pattern.compile("([a-zA-Z0-9]+)([\\s]+)([a-zA-Z ]+)([\\s]+)([0-9]+)");
         String line = "!* UserName10 John Smith 01123 *!";
         Matcher matcher = pattern.matcher(line);
         System.out.println("groupCount="+matcher.groupCount());
         if (matcher.find())
         {
             System.out.println("group = "+ matcher.group());
             System.out.println("group(0) = "+ matcher.group(0));
             System.out.println("group(1) = "+ matcher.group(1));
             System.out.println("group(2) = "+ matcher.group(2));
             System.out.println("group(3) = "+ matcher.group(3));
             System.out.println("group(4) = "+ matcher.group(4));
             System.out.println("group(5) = "+ matcher.group(5));
         }

    }

}



Last Updated: June 8, 2015 4:26 AM