Read the following online tutrial: Regex Tutorial. You may skip the section on Unions, Intersections and Subtractions. 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.
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.