001: /*
002: * $Id: matchesContainsExample.java,v 1.7 2003/11/07 20:16:23 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package examples.awk;
059:
060: import org.apache.oro.text.regex.*;
061: import org.apache.oro.text.awk.*;
062:
063: /**
064: * This is a test program demonstrating the difference between the
065: * OROMatcher matches() and contains() methods.
066: *
067: * @version @version@
068: */
069: public final class matchesContainsExample {
070:
071: /**
072: * A common mistake is to confuse the behavior of the matches() and
073: * contains() methods. matches() tests to see if a string exactly
074: * matches a pattern whereas contains() searches for the first pattern
075: * match contained somewhere within the string. When used with a
076: * PatternMatcherInput instance, the contains() method allows you to
077: * search for every pattern match within a string by using a while loop.
078: */
079: public static final void main(String args[]) {
080: int matches = 0;
081: String numberExpression = "\\d+";
082: String exactMatch = "2010";
083: String containsMatches = " 2001 was the movie before 2010, which takes place before 2069 the book ";
084: Pattern pattern = null;
085: PatternMatcherInput input;
086: PatternCompiler compiler;
087: PatternMatcher matcher;
088: MatchResult result;
089:
090: // Create AwkCompiler and AwkMatcher instances.
091: compiler = new AwkCompiler();
092: matcher = new AwkMatcher();
093:
094: // Attempt to compile the pattern. If the pattern is not valid,
095: // report the error and exit.
096: try {
097: pattern = compiler.compile(numberExpression);
098: } catch (MalformedPatternException e) {
099: System.err.println("Bad pattern.");
100: System.err.println(e.getMessage());
101: System.exit(1);
102: }
103:
104: // Here we show the difference between the matches() and contains()
105: // methods(). Compile the program and study the output to reinforce
106: // in your mind what the methods do.
107:
108: System.out.println("Input: " + exactMatch);
109:
110: // The following should return true because exactMatch exactly matches
111: // numberExprssion.
112:
113: if (matcher.matches(exactMatch, pattern))
114: System.out.println("matches() Result: TRUE, EXACT MATCH");
115: else
116: System.out
117: .println("matches() Result: FALSE, NOT EXACT MATCH");
118:
119: System.out.println("\nInput: " + containsMatches);
120:
121: // The following should return false because containsMatches does not
122: // exactly match numberExpression even though its subparts do.
123:
124: if (matcher.matches(containsMatches, pattern))
125: System.out.println("matches() Result: TRUE, EXACT MATCH");
126: else
127: System.out
128: .println("matches() Result: FALSE, NOT EXACT MATCH");
129:
130: // Now we call the contains() method. contains() should return true
131: // for both strings.
132:
133: System.out.println("\nInput: " + exactMatch);
134:
135: if (matcher.contains(exactMatch, pattern)) {
136: System.out.println("contains() Result: TRUE");
137:
138: // Fetch match and print.
139: result = matcher.getMatch();
140: System.out.println("Match: " + result);
141: } else
142: System.out.println("contains() Result: FALSE");
143:
144: System.out.println("\nInput: " + containsMatches);
145:
146: if (matcher.contains(containsMatches, pattern)) {
147: System.out.println("contains() Result: TRUE");
148: // Fetch match and print.
149: result = matcher.getMatch();
150: System.out.println("Match: " + result);
151: } else
152: System.out.println("contains() Result: FALSE");
153:
154: // In the previous example, notice how contains() will fetch only first
155: // match in a string. If you want to search a string for all of the
156: // matches it contains, you must create a PatternMatcherInput object
157: // to keep track of the position of the last match, so you can pick
158: // up a search where the last one left off.
159:
160: input = new PatternMatcherInput(containsMatches);
161:
162: System.out.println("\nPatternMatcherInput: " + input);
163: // Loop until there are no more matches left.
164: while (matcher.contains(input, pattern)) {
165: // Since we're still in the loop, fetch match that was found.
166: result = matcher.getMatch();
167:
168: ++matches;
169:
170: System.out.println("Match " + matches + ": " + result);
171: }
172: }
173: }
|