Filter.java


Below is the syntax highlighted version of Filter.java from §7.2 Regular Expressions.


/*************************************************************************
 *  Compilation:  javac Filter.java
 *  Execution:    java Filter < input.txt
 *  Dependencies: In.java
 *  
 *  Remove all characters that aren't alphanumeric or whitespace.
 *
 *************************************************************************/

public class Filter { 

    public static void main(String[] args) { 
        String pattern = "[^\\s0-9a-zA-Z]";  // whitespace or alpha-numeric

        // read input
        In in = new In();
        String input = in.readAll();

        // create and print output
        String output = input.replaceAll(pattern, "");
        System.out.println(output);
    }

}


Copyright © 2000–2011, Robert Sedgewick and Kevin Wayne.
Last updated: Wed Feb 9 09:20:16 EST 2011.