import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexTestHarness { public static void main(String[] args){ while (true) { String regexLine = readLine("\nEnter your regex:"); Pattern pattern = Pattern.compile(regexLine); String searchLine = readLine("Enter your input string to search: "); Matcher matcher = pattern.matcher(searchLine); boolean found = false; while (matcher.find()) { System.out.printf("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.printf("No match found.\n"); } } } private static String readLine(String prompt) { String line = null; Console c = System.console(); if (c != null) { line = c.readLine(prompt); } else { System.out.print(prompt); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); try { line = bufferedReader.readLine(); } catch (IOException e) { //Ignore } } return line; } }