import java.io.*; import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexTestHarness { public static void main(String[] args) throws IOException, NullPointerException { try { BufferedReader console = new BufferedReader(new InputStreamReader( System.in)); while (true) { System.out.format("%nEnter your regex: "); Pattern pattern = Pattern.compile(console.readLine()); System.out.format("Enter input string to search: "); Matcher matcher = pattern.matcher(console.readLine()); boolean found = false; while (matcher.find()) { System.out.format("I 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("No match found.%n"); } } } catch (NullPointerException npe) { System.out.println("\nStopped"); } } }