KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > regexp > util > RETest


1 /*
2  * gnu/regexp/util/RETest.java
3  * Copyright (C) 1998-2001 Wes Biggs
4  *
5  * This file is in the public domain. However, the gnu.regexp library
6  * proper is licensed under the terms of the GNU Lesser General Public
7  * License (see the file COPYING.LIB for details).
8  */

9 package gnu.regexp.util;
10 import gnu.regexp.*;
11
12 /**
13  * RETest provides a simple way to test regular expressions.
14  * It runs from the command line using the Java interpreter.
15  * To use it, enter the following from a command prompt (provided
16  * that the Java system knows where to find the RETest bytecodes):
17  * <BR><CODE>java gnu.regexp.util.RETest [regExp] [inputString]</CODE><BR>
18  * where <i>regExp</i> is a regular expression (you'll probably have
19  * to escape shell meta-characters) and <i>inputString</i> is the string
20  * to match against (again, put it in quotes or escape any shell meta-
21  * characters).
22  * <P>
23  * The test function will report the package version number, whether
24  * the expression matches the input string, what the match it found was,
25  * and the contents of any subexpressions, if applicable.
26  * <P>
27  * You may optionally add a third integer argument which is the number of
28  * times to repeat the test. When this option is used, RETest will report
29  * average compile and match times.
30  *
31  * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
32  * @version 1.01
33  */

34 public class RETest {
35   private RETest() { }
36
37   /**
38    * Invokes the test function with the command line arguments specified.
39    * See class description for usage notes.
40    *
41    * @param argv
42    * The command line arguments.
43    *
44    * @exception REException
45    * There was an error compiling or executing the regular expression.
46    */

47   public static void main(String JavaDoc argv[]) throws REException {
48     System.out.println("gnu.regexp version "+RE.version());
49     
50     int numRepeats = 1;
51     if (argv.length == 3)
52       numRepeats = Integer.parseInt(argv[2]);
53     if (argv.length < 2) {
54       System.out.println("usage: java gnu.regexp.util.RETest regExp inputString [numRepeats]");
55       System.exit(1);
56     }
57     
58     // Construct the regular expression
59

60     RE expression = null;
61     long begin = System.currentTimeMillis();
62
63     for (int rpt = 0; rpt < numRepeats; rpt++)
64       expression = new RE(argv[0]);
65
66     long end = System.currentTimeMillis();
67     
68     if (numRepeats>1) {
69       System.out.println("Compiling "+numRepeats+" times took "+(end-begin)+" ms");
70       System.out.println("Average compile time: "+((end-begin)/numRepeats)+" ms");
71     }
72
73     // Display regular expression
74
System.out.println(" Input Text: "+argv[1]);
75     System.out.println("Regular Expression: "+argv[0]);
76     System.out.println(" Compiled Form: "+expression);
77     System.out.println(" Minimum Length: "+expression.getMinimumLength());
78
79     // Is the input in its entirety a match?
80
System.out.println(" isMatch() returns: "+expression.isMatch(argv[1]));
81     
82     REMatch[] matches = expression.getAllMatches(argv[1]);
83     System.out.println(" getAllMatches(): " + matches.length + " matches");
84     for (int i = 0; i < matches.length; i++) {
85       System.out.println("Match " + i + " (" + matches[i].getStartIndex()
86              + "," + matches[i].getEndIndex() + "): "
87              + matches[i]);
88     }
89
90     // Get the first match
91
REMatch match = null;
92
93     begin = System.currentTimeMillis();
94
95     for (int rpt = 0; rpt < numRepeats; rpt++)
96       match = expression.getMatch(argv[1]);
97
98     end = System.currentTimeMillis();
99
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 }
138
Popular Tags