Validate.java


Below is the syntax highlighted version of Validate.java from §5.4 Regular Expressions.


/******************************************************************************
 *  Compilation:  javac Validate.java
 *  Execution:    java Validate pattern text
 *  Dependencies: StdOut.java
 *
 *  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 "..oo..oo." bloodroot
 *  true
 *
 *  % java Validate "..oo..oo." nincompoophood
 *  false
 *
 *  % java Validate "[^aeiou]{6}" rhythm
 *  true
 *
 *  % java Validate "[^aeiou]{6}" rhythms
 *  false
 *
 ******************************************************************************/

public class Validate {

    public static void main(String[] args) {
        String regexp = args[0];
        String text   = args[1];
        StdOut.println(text.matches(regexp));
    }

}


Copyright © 2000–2019, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 09:35:22 EDT 2022.