KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > matchesContainsExample


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
27  * must not be used to endorse or promote products derived from this
28  * software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
33  * name, without prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon software originally written
55  * by Daniel F. Savarese. We appreciate his contributions.
56  */

57
58 import org.apache.oro.text.regex.*;
59
60 /**
61  * This is a test program demonstrating the difference between the
62  * matches() and contains() methods.
63  *
64  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
65  @version $Id: matchesContainsExample.java,v 1.1.1.1 2000/07/23 23:08:48 jon Exp $
66  */

67 public final class matchesContainsExample {
68
69   /**
70    * A common mistake is to confuse the behavior of the matches() and
71    * contains() methods. matches() tests to see if a string exactly
72    * matches a pattern whereas contains() searches for the first pattern
73    * match contained somewhere within the string. When used with a
74    * PatternMatcherInput instance, the contains() method allows you to
75    * search for every pattern match within a string by using a while loop.
76    */

77   public static final void main(String JavaDoc args[]) {
78     int matches = 0;
79     String JavaDoc numberExpression = "\\d+";
80     String JavaDoc exactMatch = "2010";
81     String JavaDoc 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     // Create Perl5Compiler and Perl5Matcher instances.
90
compiler = new Perl5Compiler();
91     matcher = new Perl5Matcher();
92
93     // Attempt to compile the pattern. If the pattern is not valid,
94
// report the error and exit.
95
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     // Here we show the difference between the matches() and contains()
104
// methods(). Compile the program and study the output to reinforce
105
// in your mind what the methods do.
106

107     System.out.println("Input: " + exactMatch);
108
109     // The following should return true because exactMatch exactly matches
110
// numberExprssion.
111

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     // The following should return false because containsMatches does not
120
// exactly match numberExpression even though its subparts do.
121

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     // Now we call the contains() method. contains() should return true
129
// for both strings.
130

131     System.out.println("\nInput: " + exactMatch);
132
133     if(matcher.contains(exactMatch, pattern)) {
134       System.out.println("contains() Result: TRUE");
135
136       // Fetch match and print.
137
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       // Fetch match and print.
147
result = matcher.getMatch();
148       System.out.println("Match: " + result);
149     } else
150       System.out.println("contains() Result: FALSE");
151
152
153     // In the previous example, notice how contains() will fetch only first
154
// match in a string. If you want to search a string for all of the
155
// matches it contains, you must create a PatternMatcherInput object
156
// to keep track of the position of the last match, so you can pick
157
// up a search where the last one left off.
158

159     input = new PatternMatcherInput(containsMatches);
160
161     System.out.println("\nPatternMatcherInput: " + input);
162     // Loop until there are no more matches left.
163
while(matcher.contains(input, pattern)) {
164       // Since we're still in the loop, fetch match that was found.
165
result = matcher.getMatch();
166
167       ++matches;
168
169       System.out.println("Match " + matches + ": " + result);
170     }
171   }
172 }
173
Popular Tags