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–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:31:22 EDT 2022.