1 57 58 import org.apache.oro.text.regex.*; 59 60 67 public final class matchesContainsExample { 68 69 77 public static final void main(String args[]) { 78 int matches = 0; 79 String numberExpression = "\\d+"; 80 String exactMatch = "2010"; 81 String containsMatches = 82 " 2001 was the movie before 2010, which takes place before 2069 the book "; 83 Pattern pattern = null; 84 PatternMatcherInput input; 85 PatternCompiler compiler; 86 PatternMatcher matcher; 87 MatchResult result; 88 89 compiler = new Perl5Compiler(); 91 matcher = new Perl5Matcher(); 92 93 try { 96 pattern = compiler.compile(numberExpression); 97 } catch(MalformedPatternException e) { 98 System.err.println("Bad pattern."); 99 System.err.println(e.getMessage()); 100 System.exit(1); 101 } 102 103 107 System.out.println("Input: " + exactMatch); 108 109 112 if(matcher.matches(exactMatch, pattern)) 113 System.out.println("matches() Result: TRUE, EXACT MATCH"); 114 else 115 System.out.println("matches() Result: FALSE, NOT EXACT MATCH"); 116 117 System.out.println("\nInput: " + containsMatches); 118 119 122 if(matcher.matches(containsMatches, pattern)) 123 System.out.println("matches() Result: TRUE, EXACT MATCH"); 124 else 125 System.out.println("matches() Result: FALSE, NOT EXACT MATCH"); 126 127 128 131 System.out.println("\nInput: " + exactMatch); 132 133 if(matcher.contains(exactMatch, pattern)) { 134 System.out.println("contains() Result: TRUE"); 135 136 result = matcher.getMatch(); 138 System.out.println("Match: " + result); 139 } else 140 System.out.println("contains() Result: FALSE"); 141 142 System.out.println("\nInput: " + containsMatches); 143 144 if(matcher.contains(containsMatches, pattern)) { 145 System.out.println("contains() Result: TRUE"); 146 result = matcher.getMatch(); 148 System.out.println("Match: " + result); 149 } else 150 System.out.println("contains() Result: FALSE"); 151 152 153 159 input = new PatternMatcherInput(containsMatches); 160 161 System.out.println("\nPatternMatcherInput: " + input); 162 while(matcher.contains(input, pattern)) { 164 result = matcher.getMatch(); 166 167 ++matches; 168 169 System.out.println("Match " + matches + ": " + result); 170 } 171 } 172 } 173
| Popular Tags
|