Validate.java


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


/******************************************************************************
 *  Compilation:  javac Validate.java
 *  Execution:    java Validate pattern < input.txt
 *  
 *  Reads in a regular expression and a text input string from the
 *  command line and prints true or false depending on whether
 *  the pattern matches the text.
 *
 *  % java Validate "(a|b)*a(a|b)(a|b)(a|b)(a|b)"
 *  bbbabbbb
 *  [Yes]
 *  bbbbbbbba
 *  [No]
 *
 *  % java Validate "..oo..oo."
 *  bloodroot
 *  [Yes]
 *  nincompoophood
 *  [False]
 *
 *  % java Validate "[^aeiou]{6}"
 *  rhythm
 *  [Yes]
 *  rhythms
 *  [No]
 *
 ******************************************************************************/

public class Validate { 

    public static void main(String[] args) { 
        String regexp = args[0];
        while (!StdIn.isEmpty()) {
            String text = StdIn.readString();
            if (text.matches(regexp)) StdOut.println("[Yes]");
            else                      StdOut.println("[No]");
        }
    }

}


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