Validate.java


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


/*************************************************************************
 *  Compilation:  javac Validate.java
 *  Execution:    java Validate pattern text
 *  
 *  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];
        System.out.println(text.matches(regexp));
    }

}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.