public static final void main(String[] args) {
int matches = 0;
String numberExpression = "\\d+";
String exactMatch = "2010";
String containsMatches =
" 2001 was the movie before 2010, which takes place before 2069 the book ";
Pattern pattern = null;
PatternMatcherInput input;
PatternCompiler compiler;
PatternMatcher matcher;
MatchResult result;
// Create AwkCompiler and AwkMatcher instances.
compiler = new AwkCompiler();
matcher = new AwkMatcher();
// Attempt to compile the pattern. If the pattern is not valid,
// report the error and exit.
try {
pattern = compiler.compile(numberExpression);
} catch(MalformedPatternException e) {
System.err.println("Bad pattern.");
System.err.println(e.getMessage());
System.exit(1);
}
// Here we show the difference between the matches() and contains()
// methods(). Compile the program and study the output to reinforce
// in your mind what the methods do.
System.out.println("Input: " + exactMatch);
// The following should return true because exactMatch exactly matches
// numberExprssion.
if(matcher.matches(exactMatch, pattern))
System.out.println("matches() Result: TRUE, EXACT MATCH");
else
System.out.println("matches() Result: FALSE, NOT EXACT MATCH");
System.out.println("\nInput: " + containsMatches);
// The following should return false because containsMatches does not
// exactly match numberExpression even though its subparts do.
if(matcher.matches(containsMatches, pattern))
System.out.println("matches() Result: TRUE, EXACT MATCH");
else
System.out.println("matches() Result: FALSE, NOT EXACT MATCH");
// Now we call the contains() method. contains() should return true
// for both strings.
System.out.println("\nInput: " + exactMatch);
if(matcher.contains(exactMatch, pattern)) {
System.out.println("contains() Result: TRUE");
// Fetch match and print.
result = matcher.getMatch();
System.out.println("Match: " + result);
} else
System.out.println("contains() Result: FALSE");
System.out.println("\nInput: " + containsMatches);
if(matcher.contains(containsMatches, pattern)) {
System.out.println("contains() Result: TRUE");
// Fetch match and print.
result = matcher.getMatch();
System.out.println("Match: " + result);
} else
System.out.println("contains() Result: FALSE");
// In the previous example, notice how contains() will fetch only first
// match in a string. If you want to search a string for all of the
// matches it contains, you must create a PatternMatcherInput object
// to keep track of the position of the last match, so you can pick
// up a search where the last one left off.
input = new PatternMatcherInput(containsMatches);
System.out.println("\nPatternMatcherInput: " + input);
// Loop until there are no more matches left.
while(matcher.contains(input, pattern)) {
// Since we're still in the loop, fetch match that was found.
result = matcher.getMatch();
++matches;
System.out.println("Match " + matches + ": " + result);
}
}
A common mistake is to confuse the behavior of the matches() and
contains() methods. matches() tests to see if a string exactly
matches a pattern whereas contains() searches for the first pattern
match contained somewhere within the string. When used with a
PatternMatcherInput instance, the contains() method allows you to
search for every pattern match within a string by using a while loop. |