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