001 /*
002 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.regex;
027
028 /**
029 * The result of a match operation.
030 *
031 * <p>This interface contains query methods used to determine the
032 * results of a match against a regular expression. The match boundaries,
033 * groups and group boundaries can be seen but not modified through
034 * a <code>MatchResult</code>.
035 *
036 * @author Michael McCloskey
037 * @version 1.12 05/05/07
038 * @see Matcher
039 * @since 1.5
040 */
041 public interface MatchResult {
042
043 /**
044 * Returns the start index of the match.
045 *
046 * @return The index of the first character matched
047 *
048 * @throws IllegalStateException
049 * If no match has yet been attempted,
050 * or if the previous match operation failed
051 */
052 public int start();
053
054 /**
055 * Returns the start index of the subsequence captured by the given group
056 * during this match.
057 *
058 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
059 * to right, starting at one. Group zero denotes the entire pattern, so
060 * the expression <i>m.</i><tt>start(0)</tt> is equivalent to
061 * <i>m.</i><tt>start()</tt>. </p>
062 *
063 * @param group
064 * The index of a capturing group in this matcher's pattern
065 *
066 * @return The index of the first character captured by the group,
067 * or <tt>-1</tt> if the match was successful but the group
068 * itself did not match anything
069 *
070 * @throws IllegalStateException
071 * If no match has yet been attempted,
072 * or if the previous match operation failed
073 *
074 * @throws IndexOutOfBoundsException
075 * If there is no capturing group in the pattern
076 * with the given index
077 */
078 public int start(int group);
079
080 /**
081 * Returns the offset after the last character matched. </p>
082 *
083 * @return @return The offset after the last character matched
084 *
085 * @throws IllegalStateException
086 * If no match has yet been attempted,
087 * or if the previous match operation failed
088 */
089 public int end();
090
091 /**
092 * Returns the offset after the last character of the subsequence
093 * captured by the given group during this match.
094 *
095 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
096 * to right, starting at one. Group zero denotes the entire pattern, so
097 * the expression <i>m.</i><tt>end(0)</tt> is equivalent to
098 * <i>m.</i><tt>end()</tt>. </p>
099 *
100 * @param group
101 * The index of a capturing group in this matcher's pattern
102 *
103 * @return The offset after the last character captured by the group,
104 * or <tt>-1</tt> if the match was successful
105 * but the group itself did not match anything
106 *
107 * @throws IllegalStateException
108 * If no match has yet been attempted,
109 * or if the previous match operation failed
110 *
111 * @throws IndexOutOfBoundsException
112 * If there is no capturing group in the pattern
113 * with the given index
114 */
115 public int end(int group);
116
117 /**
118 * Returns the input subsequence matched by the previous match.
119 *
120 * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
121 * the expressions <i>m.</i><tt>group()</tt> and
122 * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt> <i>m.</i><tt>end())</tt>
123 * are equivalent. </p>
124 *
125 * <p> Note that some patterns, for example <tt>a*</tt>, match the empty
126 * string. This method will return the empty string when the pattern
127 * successfully matches the empty string in the input. </p>
128 *
129 * @return The (possibly empty) subsequence matched by the previous match,
130 * in string form
131 *
132 * @throws IllegalStateException
133 * If no match has yet been attempted,
134 * or if the previous match operation failed
135 */
136 public String group();
137
138 /**
139 * Returns the input subsequence captured by the given group during the
140 * previous match operation.
141 *
142 * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
143 * <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
144 * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(</tt><i>g</i><tt>),</tt> <i>m.</i><tt>end(</tt><i>g</i><tt>))</tt>
145 * are equivalent. </p>
146 *
147 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
148 * to right, starting at one. Group zero denotes the entire pattern, so
149 * the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
150 * </p>
151 *
152 * <p> If the match was successful but the group specified failed to match
153 * any part of the input sequence, then <tt>null</tt> is returned. Note
154 * that some groups, for example <tt>(a*)</tt>, match the empty string.
155 * This method will return the empty string when such a group successfully
156 * matches the empty string in the input. </p>
157 *
158 * @param group
159 * The index of a capturing group in this matcher's pattern
160 *
161 * @return The (possibly empty) subsequence captured by the group
162 * during the previous match, or <tt>null</tt> if the group
163 * failed to match part of the input
164 *
165 * @throws IllegalStateException
166 * If no match has yet been attempted,
167 * or if the previous match operation failed
168 *
169 * @throws IndexOutOfBoundsException
170 * If there is no capturing group in the pattern
171 * with the given index
172 */
173 public String group(int group);
174
175 /**
176 * Returns the number of capturing groups in this match result's pattern.
177 *
178 * <p> Group zero denotes the entire pattern by convention. It is not
179 * included in this count.
180 *
181 * <p> Any non-negative integer smaller than or equal to the value
182 * returned by this method is guaranteed to be a valid group index for
183 * this matcher. </p>
184 *
185 * @return The number of capturing groups in this matcher's pattern
186 */
187 public int groupCount();
188
189 }
|