001: /*
002: * $Id: matchResultExample.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;
059:
060: import org.apache.oro.text.regex.*;
061:
062: /**
063: * This is a test program demonstrating the methods of the OROMatcher
064: * MatchResult class.
065: *
066: * @version @version@
067: */
068: public final class matchResultExample {
069:
070: /**
071: * Takes a regular expression and string as input and reports all the
072: * pattern matches in the string.
073: * <p>
074: * @param args[] The array of arguments to the program. The first
075: * argument should be a Perl5 regular expression, and the second
076: * should be an input string.
077: */
078: public static final void main(String args[]) {
079: int groups;
080: PatternMatcher matcher;
081: PatternCompiler compiler;
082: Pattern pattern = null;
083: PatternMatcherInput input;
084: MatchResult result;
085:
086: // Must have at least two arguments, else exit.
087: if (args.length < 2) {
088: System.err.println("Usage: matchResult pattern input");
089: return;
090: }
091:
092: // Create Perl5Compiler and Perl5Matcher instances.
093: compiler = new Perl5Compiler();
094: matcher = new Perl5Matcher();
095:
096: // Attempt to compile the pattern. If the pattern is not valid,
097: // report the error and exit.
098: try {
099: pattern = compiler.compile(args[0]);
100: } catch (MalformedPatternException e) {
101: System.err.println("Bad pattern.");
102: System.err.println(e.getMessage());
103: return;
104: }
105:
106: // Create a PatternMatcherInput instance to keep track of the position
107: // where the last match finished, so that the next match search will
108: // start from there. You always create a PatternMatcherInput instance
109: // when you want to search a string for all of the matches it contains,
110: // and not just the first one.
111: input = new PatternMatcherInput(args[1]);
112:
113: // Loop until there are no more matches left.
114: while (matcher.contains(input, pattern)) {
115: // Since we're still in the loop, fetch match that was found.
116: result = matcher.getMatch();
117:
118: // Perform whatever processing on the result you want.
119: // Here we just print out all its elements to show how the
120: // MatchResult methods are used.
121:
122: // The toString() method is provided as a convenience method.
123: // It returns the entire match. The following are all equivalent:
124: // System.out.println("Match: " + result);
125: // System.out.println("Match: " + result.toString());
126: // System.out.println("Match: " + result.group(0));
127: System.out.println("Match: " + result.toString());
128:
129: // Print the length of the match. The length() method is another
130: // convenience method. The lengths of subgroups can be obtained
131: // by first retrieving the subgroup and then calling the string's
132: // length() method.
133: System.out.println("Length: " + result.length());
134:
135: // Retrieve the number of matched groups. A group corresponds to
136: // a parenthesized set in a pattern.
137: groups = result.groups();
138: System.out.println("Groups: " + groups);
139:
140: // Print the offset into the input of the beginning and end of the
141: // match. The beinOffset() and endOffset() methods return the
142: // offsets of a group relative to the beginning of the input. The
143: // begin() and end() methods return the offsets of a group relative
144: // the to the beginning of a match.
145: System.out
146: .println("Begin offset: " + result.beginOffset(0));
147: System.out.println("End offset: " + result.endOffset(0));
148: System.out.println("Groups: ");
149:
150: // Print the contents of each matched subgroup along with their
151: // offsets relative to the beginning of the entire match.
152:
153: // Start at 1 because we just printed out group 0
154: for (int group = 1; group < groups; group++) {
155: System.out.println(group + ": " + result.group(group));
156: System.out.println("Begin: " + result.begin(group));
157: System.out.println("End: " + result.end(group));
158: }
159: }
160: }
161: }
|