1 9 package gnu.regexp.util; 10 import gnu.regexp.*; 11 12 34 public class RETest { 35 private RETest() { } 36 37 47 public static void main(String 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 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 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 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 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 110 System.out.println("Match found from position " 111 + match.getStartIndex() + " to position " 112 + match.getEndIndex()); 113 114 116 System.out.println("Match was: '" + match + "'"); 117 118 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 128 System.out.println(match.substituteInto("The subexpression matched this text: '$"+i+"'")); 129 } 130 } 131 } 132 133 System.out.println("substitute(): " + expression.substitute(argv[1],"<!--$0-->")); 135 System.out.println("substituteAll(): " + expression.substituteAll(argv[1],"<!--$0-->")); 136 } 137 } 138 | Popular Tags |