2 * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.util.regex;
29 * The result of a match operation.
31 * <p>This interface contains query methods used to determine the
32 * results of a match against a regular expression. The match boundaries,
33 * groups and group boundaries can be seen but not modified through
34 * a <code>MatchResult</code>.
36 * @author Michael McCloskey
40 public interface MatchResult {
43 * Returns the start index of the match.
45 * @return The index of the first character matched
47 * @throws IllegalStateException
48 * If no match has yet been attempted,
49 * or if the previous match operation failed
54 * Returns the start index of the subsequence captured by the given group
57 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
58 * to right, starting at one. Group zero denotes the entire pattern, so
59 * the expression <i>m.</i><tt>start(0)</tt> is equivalent to
60 * <i>m.</i><tt>start()</tt>. </p>
63 * The index of a capturing group in this matcher's pattern
65 * @return The index of the first character captured by the group,
66 * or <tt>-1</tt> if the match was successful but the group
67 * itself did not match anything
69 * @throws IllegalStateException
70 * If no match has yet been attempted,
71 * or if the previous match operation failed
73 * @throws IndexOutOfBoundsException
74 * If there is no capturing group in the pattern
75 * with the given index
77 public int start(int group);
80 * Returns the offset after the last character matched. </p>
82 * @return @return The offset after the last character matched
84 * @throws IllegalStateException
85 * If no match has yet been attempted,
86 * or if the previous match operation failed
91 * Returns the offset after the last character of the subsequence
92 * captured by the given group during this match.
94 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
95 * to right, starting at one. Group zero denotes the entire pattern, so
96 * the expression <i>m.</i><tt>end(0)</tt> is equivalent to
97 * <i>m.</i><tt>end()</tt>. </p>
100 * The index of a capturing group in this matcher's pattern
102 * @return The offset after the last character captured by the group,
103 * or <tt>-1</tt> if the match was successful
104 * but the group itself did not match anything
106 * @throws IllegalStateException
107 * If no match has yet been attempted,
108 * or if the previous match operation failed
110 * @throws IndexOutOfBoundsException
111 * If there is no capturing group in the pattern
112 * with the given index
114 public int end(int group);
117 * Returns the input subsequence matched by the previous match.
119 * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
120 * the expressions <i>m.</i><tt>group()</tt> and
121 * <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt> <i>m.</i><tt>end())</tt>
122 * are equivalent. </p>
124 * <p> Note that some patterns, for example <tt>a*</tt>, match the empty
125 * string. This method will return the empty string when the pattern
126 * successfully matches the empty string in the input. </p>
128 * @return The (possibly empty) subsequence matched by the previous match,
131 * @throws IllegalStateException
132 * If no match has yet been attempted,
133 * or if the previous match operation failed
135 public String group();
138 * Returns the input subsequence captured by the given group during the
139 * previous match operation.
141 * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
142 * <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
143 * <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>
144 * are equivalent. </p>
146 * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
147 * to right, starting at one. Group zero denotes the entire pattern, so
148 * the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
151 * <p> If the match was successful but the group specified failed to match
152 * any part of the input sequence, then <tt>null</tt> is returned. Note
153 * that some groups, for example <tt>(a*)</tt>, match the empty string.
154 * This method will return the empty string when such a group successfully
155 * matches the empty string in the input. </p>
158 * The index of a capturing group in this matcher's pattern
160 * @return The (possibly empty) subsequence captured by the group
161 * during the previous match, or <tt>null</tt> if the group
162 * failed to match part of the input
164 * @throws IllegalStateException
165 * If no match has yet been attempted,
166 * or if the previous match operation failed
168 * @throws IndexOutOfBoundsException
169 * If there is no capturing group in the pattern
170 * with the given index
172 public String group(int group);
175 * Returns the number of capturing groups in this match result's pattern.
177 * <p> Group zero denotes the entire pattern by convention. It is not
178 * included in this count.
180 * <p> Any non-negative integer smaller than or equal to the value
181 * returned by this method is guaranteed to be a valid group index for
184 * @return The number of capturing groups in this matcher's pattern
186 public int groupCount();