package examples; import java.util.Scanner; import java.util.regex.PatternSyntaxException; public class InteractiveScanner { public static void main(String[] args) { Scanner s = new Scanner(System.in); // scan the standard input stream String text = ""; String pattern = "[a-z]+"; while (true) { // prompt for input System.out.print("> "); System.out.flush(); // consume command code (t/text, p/pattern, e/exit) and update state String type = s.next().toLowerCase(); if (type.equals("t") || type.equals("text")) { text = s.nextLine().trim(); } else if (type.equals("p") || type.equals("pattern")) { pattern = s.nextLine().trim(); } else if (type.equals("e") || type.equals("exit")) { System.exit(0); } // perform scan of text using the pattern try { System.out.println("With pattern [" + pattern + "], scanning: " + text); Scanner textScan = new Scanner(text); String result = null; while ((result = textScan.findInLine(pattern)) != null && result.length() > 0) System.out.println(result); } catch (PatternSyntaxException pse) { System.out.println("Illegal pattern " + pattern); System.out.println(pse.getMessage()); pattern = ""; } } } }