001/*
002 *  gnu/regexp/util/RETest.java
003 *  Copyright (C) 1998-2001 Wes Biggs
004 *
005 *  This file is in the public domain.  However, the gnu.regexp library
006 *  proper is licensed under the terms of the GNU Lesser General Public
007 *  License (see the file COPYING.LIB for details).
008 */
009package gnu.regexp.util;
010import gnu.regexp.*;
011
012/**
013 *  RETest provides a simple way to test regular expressions.
014 *  It runs from the command line using the Java interpreter.
015 *  To use it, enter the following from a command prompt (provided
016 *  that the Java system knows where to find the RETest bytecodes):
017 *  <BR><CODE>java gnu.regexp.util.RETest [regExp] [inputString]</CODE><BR>
018 *  where <i>regExp</i> is a regular expression (you'll probably have
019 *  to escape shell meta-characters) and <i>inputString</i> is the string
020 *  to match against (again, put it in quotes or escape any shell meta-
021 *  characters).
022 *  <P>
023 *  The test function will report the package version number, whether
024 *  the expression matches the input string, what the match it found was,
025 *  and the contents of any subexpressions, if applicable.
026 *  <P>
027 *  You may optionally add a third integer argument which is the number of
028 *  times to repeat the test.  When this option is used, RETest will report
029 *  average compile and match times.
030 *
031 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
032 * @version 1.01
033 */
034public class RETest {
035  private RETest() { }
036
037  /**
038   * Invokes the test function with the command line arguments specified.
039   * See class description for usage notes.
040   *
041   * @param argv
042   * The command line arguments.
043   *
044   * @exception REException
045   * There was an error compiling or executing the regular expression.
046   */
047  public static void main(String argv[]) throws REException {
048    System.out.println("gnu.regexp version "+RE.version());
049    
050    int numRepeats = 1;
051    if (argv.length == 3)
052      numRepeats = Integer.parseInt(argv[2]);
053    if (argv.length < 2) {
054      System.out.println("usage: java gnu.regexp.util.RETest regExp inputString [numRepeats]");
055      System.exit(1);
056    }
057    
058    // Construct the regular expression
059
060    RE expression = null;
061    long begin = System.currentTimeMillis();
062
063    for (int rpt = 0; rpt < numRepeats; rpt++)
064      expression = new RE(argv[0]);
065
066    long end = System.currentTimeMillis();
067    
068    if (numRepeats>1) {
069      System.out.println("Compiling "+numRepeats+" times took "+(end-begin)+" ms");
070      System.out.println("Average compile time: "+((end-begin)/numRepeats)+" ms");
071    }
072
073    // Display regular expression
074    System.out.println("        Input Text: "+argv[1]);
075    System.out.println("Regular Expression: "+argv[0]);
076    System.out.println("     Compiled Form: "+expression);
077    System.out.println("    Minimum Length: "+expression.getMinimumLength());
078
079    // Is the input in its entirety a match?
080    System.out.println(" isMatch() returns: "+expression.isMatch(argv[1]));
081    
082    REMatch[] matches = expression.getAllMatches(argv[1]);
083    System.out.println("   getAllMatches(): " + matches.length + " matches");
084    for (int i = 0; i < matches.length; i++) {
085      System.out.println("Match " + i + " (" + matches[i].getStartIndex()
086                         + "," + matches[i].getEndIndex() + "): "
087                         + matches[i]);
088    }
089
090    // Get the first match    
091    REMatch match = null;
092
093    begin = System.currentTimeMillis();
094
095    for (int rpt = 0; rpt < numRepeats; rpt++)
096      match = expression.getMatch(argv[1]);
097
098    end = System.currentTimeMillis();
099
100    if (numRepeats>1) {
101      System.out.println("Finding first match "+numRepeats+" times took "+(end-begin)+" ms");
102      System.out.println("Average match time: "+((end-begin)/numRepeats)+" ms");
103    }
104
105    if (match == null)
106      System.out.println("Expression did not find a match.");
107    else {
108      // Report the full match indices
109
110      System.out.println("Match found from position "
111                         + match.getStartIndex() + " to position "
112                         + match.getEndIndex());
113      
114      // Take advantage of REMatch.toString() to print match text
115      
116      System.out.println("Match was: '" + match + "'");
117      
118      // Report subexpression positions
119      
120      for (int i=1; i <= expression.getNumSubs(); i++) {
121        if (match.getStartIndex(i) > -1) {
122          System.out.println("Subexpression #" + i + ": from position "
123                             + match.getStartIndex(i) + " to position "
124                             + match.getEndIndex(i));
125                
126          // Note how the $n is constructed for substituteInto
127                
128          System.out.println(match.substituteInto("The subexpression matched this text: '$"+i+"'"));
129        }
130      }
131    }
132
133    // Here's a substitute test.
134    System.out.println("substitute(): " + expression.substitute(argv[1],"<!--$0-->"));
135    System.out.println("substituteAll(): " + expression.substituteAll(argv[1],"<!--$0-->"));
136  }
137}