CGS4854 Regular Expression Tutorial

Due Wednesday, March 7, before class starts

I will be out of town on Wednesday, March 9.

In lieu of attending class, I would like you all to review the following tutorial on regular expressions.

This will count as extra credit.

Regex Tutorial. You only have to review through the section on Boundaries. The sections after this show how to write Java code that uses regular expressions. Of course, feel free to do the entire tutorial and read up on the Java syntax and classes.

The test harness that is given in the tutorial does not work in NetBeans. Instead, you may download the following test harness from my web site.

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

public class RegexTestHarness {

    Pattern pattern;
    String line;
    Scanner console;

    public RegexTestHarness() {

    }

    public static void main(String[] args){
        new RegexTestHarness().run();
    }

    public void run() {
        console = new Scanner(System.in);
        if (console == null) {
            System.err.println("No console.");
            System.exit(1);
        }
        boolean running = true;
        while (running) {
            System.out.format("%nEnter your regex (enter * to exit): ");
            line = console.nextLine();
            if (line.length() > 1 || !line.equals("*")) {
                pattern = Pattern.compile(line);

                System.out.format("\tEnter input string to search (enter * to enter new regex): ");
                line = console.nextLine();
                while (line.length() > 1 || !line.equals("*")) {
                    testPattern(line);
                    System.out.format("\tEnter input string to search (enter * to enter new regex): ");
                    line = console.nextLine();
                }
            } else {
                running = false;
            }
        }
    }

    void testPattern(String line) {
        Matcher matcher =
        pattern.matcher(line);

        boolean found = false;
        while (matcher.find()) {
            System.out.format("\t\tI found the text \"%s\" starting at " +
               "index %d and ending at index %d.%n",
                matcher.group(), matcher.start(), matcher.end());
            found = true;
        }
        if(!found){
            System.out.format("\t\tNo match found.%n");
        }
    }
}

This tutorial should help you understand regular expressions, so you will get the regular expression questions correct on the next exam.

After you have read the tutorial, do the following. Be sure to test your answers using the test harness program, so you know that it is correct.

  1. Write the regular expression that will match any line that contains a word that has the string mat in it. Examples that should match: a) I like mathematics; b) more enigmatic than not; c) I ate at the automat on Tuesday.
  2. Write the regular expression that will match any line that contains a word that begins with mat. Examples that should match: a) I like mathematics; b) Matt is unhappy. Examples that do not match: c) more enigmatic than not; d) I ate at the automat on Tuesday.
  3. Write the regular expression that will match any line that contains a word that ends with mat. Examples that should match: a) I ate at the automat on Tuesday; b) The mat was blue. Examples that do not match: c) more enigmatic than not; d) I like mathematics.
  4. Write the regular expression that will that will match any line that is exactly one word of lower case letters that do no contain the vowels i, o, u. Examples that match: a) read; b) dear. Examples that do not match: c) dear me; d) received.
  5. Write the shortest regular expression that matches any line that is exactly one of the words ran, ram, ron, rom, run, rum, tan, tam, ton, tom, tun, tum. Examples that match: a) ran; b) rum; c)tam. Examples that do not match: d) RUN; e) run tam; f) help.
  6. Write the regular expression that matches any line that contains exactly two names separated by white space characters. Names can only contain upper and lower case letters. Examples that match: a) Fred FLINTstone; b) Laura CrOfT. Examples that do not match: c) B B King; d) Cher.
  7. Write the regular expression that matches any line that is exactly one word that has between one and five lower case letters followed by three digits. Examples that match: a) hello123 b) bob999. Examples that do not match: c) fergie123 d) pen23.

Write the answers in a text file. If you use a word processor, be sure to save the file as text. Upload the file to me: Submitting homework on-line via the Web.