SearchAndReplace.java


Below is the syntax highlighted version of SearchAndReplace.java from §5.1 Formal Languages.


/******************************************************************************
 *  Compilation:  javac SearchAndReplace.java In.java
 *  Execution:    java SearchAndReplace from to < input.txt
 *  
 *  Replaces all occurrence of the "from" string with the "to"
 *  string.
 *
 ******************************************************************************/

public class SearchAndReplace { 


    public static void main(String[] args) { 
        String regexp = args[0];
        String to = args[1];

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

        // create and print output
        String output = input.replaceAll(regexp, to);
        StdOut.println(output);
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.