/* * Using regular expressions in Java * * Example program from Adam Brooks Weber, * Formal Language: A Practical Introduction, * pages 96-97 * * With additional comments added by Bill Slough * * See: * http://download.oracle.com/javase/tutorial/essential/regex/ * * for many more details of regular expressions in Java. */ import java.io.*; import java.util.regex.*; public class RegexFilter { public static void main(String[] args) throws IOException { // Get a regular expression from the command line argument Pattern p = Pattern.compile(args[0]); // Obtain input from standard input BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Process the input file, one line at a time String s = in.readLine(); // get the next line, if possible while (s != null) { // Create a matcher that builds an NFA for the given pattern p Matcher m = p.matcher(s); // Echo the input line if it matches the regular expression if (m.matches()) { System.out.println(s); } s = in.readLine(); // get the next line } } }