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

String RegEx capability

The String class has some powerful capabilities that are based on Regular Expressions. Regular Expressions shows up a lot in the Unix/Linux world and this topic can be very complex. This is a topic that could easily have a whole book devoted to it.

In this example I want to demonstrate a few methods in the String class based on Regular Expressions: replaceAll and split.

We will give more Regular Expression examples in subsequent programs. The guts to the Java handling of Regular Expressions can be found in the class:

java.util.regex.Pattern - look this class up in the Java API documentation for more extensive documentation on Regular Expressions.

String replaceAll( String regularExpression, String replacementString);

String[] split (String regularExpression);

More regular expression examples will show up in the next lectures, but for the following code I will use the following regular expressions:

Reg Ex as String | Reg Ex read from keyboard | Definition of what the Reg Ex does
"cat" | cat | Just what you think
"\\s" | \s | One white-space character. Example: space, tab, new-line
"\\s+" | \s+ | One or more white-space characters
"[,;\\s\\.]+" | [,;\s\.]+ | One or more of the characters in the following set: , ; . white-space

StringRegEx.java



import java.util.Scanner;

public class StringRegEx {

    static void hardwired_test()
    {
        System.out.println("\n\n*******  Replace Example2 ***********");
        String text="one   cat  two cats   in the yard full of cats     in a world of cats";
        // "cat" is a regular expression
        // This will replace all occurrences of "cat" with "dog"
        String s2=text.replaceAll("cat", "dog");
        System.out.println(s2);

        // This will replace every white-space character with a '.'
        s2 = text.replaceAll("\\s", ".");
        System.out.println(s2);

        // This will replace every sequence(1 or more) of white-space
       //  characters with a '.'
        s2 = text.replaceAll("\\s+", ".");
        System.out.println(s2);

        System.out.println("\n\n*******   String Split examples *******");

        text = "wheat    barley corn oats    rice";
        // The words returned are the words
        // returned from a StringTokenizer with specifying a delimiter string.
        // This will return all of words using white space as the
        // splitting delimiter.
        String[]arr = text.split("\\s+");
        for (int i=0; i < arr.length; i++)
        {
            System.out.println(arr[i]);
        }

        System.out.println("\n  Second Split example  ");
        text = "wheat.germ    barley, corn; oats.    rice";
        // This will return the words that a StringTokenizer would
        // return with delimiters defined as ",; ."
        arr = text.split("[,;\\s\\.]+");
        for (int i=0; i < arr.length; i++)
        {
            System.out.println(arr[i]);
        }
    }

    public static void main(String[] args) {
        hardwired_test();

        Scanner keyboard = new Scanner(System.in);
        while(true)
        {
            System.out.println("Enter the text");
            String text = keyboard.nextLine();
            System.out.println("Enter the Regular Expression");
            String regex = keyboard.nextLine();

            System.out.println("splitting on regex string: "+regex);
            String[] arr = text.split(regex);
            for (int i=0; i < arr.length; i++)
                System.out.println(arr[i]);
        }

    }

}



Last Updated: June 6, 2015 7:25 PM