jtulach@1348
|
1 |
/*
|
jtulach@1348
|
2 |
* Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1348
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1348
|
4 |
*
|
jtulach@1348
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1348
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1348
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1348
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1348
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1348
|
10 |
*
|
jtulach@1348
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1348
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1348
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1348
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1348
|
15 |
* accompanied this code).
|
jtulach@1348
|
16 |
*
|
jtulach@1348
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1348
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1348
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1348
|
20 |
*
|
jtulach@1348
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1348
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1348
|
23 |
* questions.
|
jtulach@1348
|
24 |
*/
|
jtulach@1348
|
25 |
|
jtulach@1348
|
26 |
package java.util.regex;
|
jtulach@1348
|
27 |
|
jtulach@1348
|
28 |
|
jtulach@1348
|
29 |
/**
|
jtulach@1348
|
30 |
* An engine that performs match operations on a {@link java.lang.CharSequence
|
jtulach@1348
|
31 |
* </code>character sequence<code>} by interpreting a {@link Pattern}.
|
jtulach@1348
|
32 |
*
|
jtulach@1348
|
33 |
* <p> A matcher is created from a pattern by invoking the pattern's {@link
|
jtulach@1348
|
34 |
* Pattern#matcher matcher} method. Once created, a matcher can be used to
|
jtulach@1348
|
35 |
* perform three different kinds of match operations:
|
jtulach@1348
|
36 |
*
|
jtulach@1348
|
37 |
* <ul>
|
jtulach@1348
|
38 |
*
|
jtulach@1348
|
39 |
* <li><p> The {@link #matches matches} method attempts to match the entire
|
jtulach@1348
|
40 |
* input sequence against the pattern. </p></li>
|
jtulach@1348
|
41 |
*
|
jtulach@1348
|
42 |
* <li><p> The {@link #lookingAt lookingAt} method attempts to match the
|
jtulach@1348
|
43 |
* input sequence, starting at the beginning, against the pattern. </p></li>
|
jtulach@1348
|
44 |
*
|
jtulach@1348
|
45 |
* <li><p> The {@link #find find} method scans the input sequence looking for
|
jtulach@1348
|
46 |
* the next subsequence that matches the pattern. </p></li>
|
jtulach@1348
|
47 |
*
|
jtulach@1348
|
48 |
* </ul>
|
jtulach@1348
|
49 |
*
|
jtulach@1348
|
50 |
* <p> Each of these methods returns a boolean indicating success or failure.
|
jtulach@1348
|
51 |
* More information about a successful match can be obtained by querying the
|
jtulach@1348
|
52 |
* state of the matcher.
|
jtulach@1348
|
53 |
*
|
jtulach@1348
|
54 |
* <p> A matcher finds matches in a subset of its input called the
|
jtulach@1348
|
55 |
* <i>region</i>. By default, the region contains all of the matcher's input.
|
jtulach@1348
|
56 |
* The region can be modified via the{@link #region region} method and queried
|
jtulach@1348
|
57 |
* via the {@link #regionStart regionStart} and {@link #regionEnd regionEnd}
|
jtulach@1348
|
58 |
* methods. The way that the region boundaries interact with some pattern
|
jtulach@1348
|
59 |
* constructs can be changed. See {@link #useAnchoringBounds
|
jtulach@1348
|
60 |
* useAnchoringBounds} and {@link #useTransparentBounds useTransparentBounds}
|
jtulach@1348
|
61 |
* for more details.
|
jtulach@1348
|
62 |
*
|
jtulach@1348
|
63 |
* <p> This class also defines methods for replacing matched subsequences with
|
jtulach@1348
|
64 |
* new strings whose contents can, if desired, be computed from the match
|
jtulach@1348
|
65 |
* result. The {@link #appendReplacement appendReplacement} and {@link
|
jtulach@1348
|
66 |
* #appendTail appendTail} methods can be used in tandem in order to collect
|
jtulach@1348
|
67 |
* the result into an existing string buffer, or the more convenient {@link
|
jtulach@1348
|
68 |
* #replaceAll replaceAll} method can be used to create a string in which every
|
jtulach@1348
|
69 |
* matching subsequence in the input sequence is replaced.
|
jtulach@1348
|
70 |
*
|
jtulach@1348
|
71 |
* <p> The explicit state of a matcher includes the start and end indices of
|
jtulach@1348
|
72 |
* the most recent successful match. It also includes the start and end
|
jtulach@1348
|
73 |
* indices of the input subsequence captured by each <a
|
jtulach@1348
|
74 |
* href="Pattern.html#cg">capturing group</a> in the pattern as well as a total
|
jtulach@1348
|
75 |
* count of such subsequences. As a convenience, methods are also provided for
|
jtulach@1348
|
76 |
* returning these captured subsequences in string form.
|
jtulach@1348
|
77 |
*
|
jtulach@1348
|
78 |
* <p> The explicit state of a matcher is initially undefined; attempting to
|
jtulach@1348
|
79 |
* query any part of it before a successful match will cause an {@link
|
jtulach@1348
|
80 |
* IllegalStateException} to be thrown. The explicit state of a matcher is
|
jtulach@1348
|
81 |
* recomputed by every match operation.
|
jtulach@1348
|
82 |
*
|
jtulach@1348
|
83 |
* <p> The implicit state of a matcher includes the input character sequence as
|
jtulach@1348
|
84 |
* well as the <i>append position</i>, which is initially zero and is updated
|
jtulach@1348
|
85 |
* by the {@link #appendReplacement appendReplacement} method.
|
jtulach@1348
|
86 |
*
|
jtulach@1348
|
87 |
* <p> A matcher may be reset explicitly by invoking its {@link #reset()}
|
jtulach@1348
|
88 |
* method or, if a new input sequence is desired, its {@link
|
jtulach@1348
|
89 |
* #reset(java.lang.CharSequence) reset(CharSequence)} method. Resetting a
|
jtulach@1348
|
90 |
* matcher discards its explicit state information and sets the append position
|
jtulach@1348
|
91 |
* to zero.
|
jtulach@1348
|
92 |
*
|
jtulach@1348
|
93 |
* <p> Instances of this class are not safe for use by multiple concurrent
|
jtulach@1348
|
94 |
* threads. </p>
|
jtulach@1348
|
95 |
*
|
jtulach@1348
|
96 |
*
|
jtulach@1348
|
97 |
* @author Mike McCloskey
|
jtulach@1348
|
98 |
* @author Mark Reinhold
|
jtulach@1348
|
99 |
* @author JSR-51 Expert Group
|
jtulach@1348
|
100 |
* @since 1.4
|
jtulach@1348
|
101 |
* @spec JSR-51
|
jtulach@1348
|
102 |
*/
|
jtulach@1348
|
103 |
|
jtulach@1348
|
104 |
public final class Matcher implements MatchResult {
|
jtulach@1348
|
105 |
|
jtulach@1348
|
106 |
/**
|
jtulach@1348
|
107 |
* The Pattern object that created this Matcher.
|
jtulach@1348
|
108 |
*/
|
jtulach@1348
|
109 |
Pattern parentPattern;
|
jtulach@1348
|
110 |
|
jtulach@1348
|
111 |
/**
|
jtulach@1348
|
112 |
* The storage used by groups. They may contain invalid values if
|
jtulach@1348
|
113 |
* a group was skipped during the matching.
|
jtulach@1348
|
114 |
*/
|
jtulach@1348
|
115 |
int[] groups;
|
jtulach@1348
|
116 |
|
jtulach@1348
|
117 |
/**
|
jtulach@1348
|
118 |
* The range within the sequence that is to be matched. Anchors
|
jtulach@1348
|
119 |
* will match at these "hard" boundaries. Changing the region
|
jtulach@1348
|
120 |
* changes these values.
|
jtulach@1348
|
121 |
*/
|
jtulach@1348
|
122 |
int from, to;
|
jtulach@1348
|
123 |
|
jtulach@1348
|
124 |
/**
|
jtulach@1348
|
125 |
* Lookbehind uses this value to ensure that the subexpression
|
jtulach@1348
|
126 |
* match ends at the point where the lookbehind was encountered.
|
jtulach@1348
|
127 |
*/
|
jtulach@1348
|
128 |
int lookbehindTo;
|
jtulach@1348
|
129 |
|
jtulach@1348
|
130 |
/**
|
jtulach@1348
|
131 |
* The original string being matched.
|
jtulach@1348
|
132 |
*/
|
jtulach@1348
|
133 |
CharSequence text;
|
jtulach@1348
|
134 |
|
jtulach@1348
|
135 |
/**
|
jtulach@1348
|
136 |
* Matcher state used by the last node. NOANCHOR is used when a
|
jtulach@1348
|
137 |
* match does not have to consume all of the input. ENDANCHOR is
|
jtulach@1348
|
138 |
* the mode used for matching all the input.
|
jtulach@1348
|
139 |
*/
|
jtulach@1348
|
140 |
static final int ENDANCHOR = 1;
|
jtulach@1348
|
141 |
static final int NOANCHOR = 0;
|
jtulach@1348
|
142 |
int acceptMode = NOANCHOR;
|
jtulach@1348
|
143 |
|
jtulach@1348
|
144 |
/**
|
jtulach@1348
|
145 |
* The range of string that last matched the pattern. If the last
|
jtulach@1348
|
146 |
* match failed then first is -1; last initially holds 0 then it
|
jtulach@1348
|
147 |
* holds the index of the end of the last match (which is where the
|
jtulach@1348
|
148 |
* next search starts).
|
jtulach@1348
|
149 |
*/
|
jtulach@1348
|
150 |
int first = -1, last = 0;
|
jtulach@1348
|
151 |
|
jtulach@1348
|
152 |
/**
|
jtulach@1348
|
153 |
* The end index of what matched in the last match operation.
|
jtulach@1348
|
154 |
*/
|
jtulach@1348
|
155 |
int oldLast = -1;
|
jtulach@1348
|
156 |
|
jtulach@1348
|
157 |
/**
|
jtulach@1348
|
158 |
* The index of the last position appended in a substitution.
|
jtulach@1348
|
159 |
*/
|
jtulach@1348
|
160 |
int lastAppendPosition = 0;
|
jtulach@1348
|
161 |
|
jtulach@1348
|
162 |
/**
|
jtulach@1348
|
163 |
* Storage used by nodes to tell what repetition they are on in
|
jtulach@1348
|
164 |
* a pattern, and where groups begin. The nodes themselves are stateless,
|
jtulach@1348
|
165 |
* so they rely on this field to hold state during a match.
|
jtulach@1348
|
166 |
*/
|
jtulach@1348
|
167 |
int[] locals;
|
jtulach@1348
|
168 |
|
jtulach@1348
|
169 |
/**
|
jtulach@1348
|
170 |
* Boolean indicating whether or not more input could change
|
jtulach@1348
|
171 |
* the results of the last match.
|
jtulach@1348
|
172 |
*
|
jtulach@1348
|
173 |
* If hitEnd is true, and a match was found, then more input
|
jtulach@1348
|
174 |
* might cause a different match to be found.
|
jtulach@1348
|
175 |
* If hitEnd is true and a match was not found, then more
|
jtulach@1348
|
176 |
* input could cause a match to be found.
|
jtulach@1348
|
177 |
* If hitEnd is false and a match was found, then more input
|
jtulach@1348
|
178 |
* will not change the match.
|
jtulach@1348
|
179 |
* If hitEnd is false and a match was not found, then more
|
jtulach@1348
|
180 |
* input will not cause a match to be found.
|
jtulach@1348
|
181 |
*/
|
jtulach@1348
|
182 |
boolean hitEnd;
|
jtulach@1348
|
183 |
|
jtulach@1348
|
184 |
/**
|
jtulach@1348
|
185 |
* Boolean indicating whether or not more input could change
|
jtulach@1348
|
186 |
* a positive match into a negative one.
|
jtulach@1348
|
187 |
*
|
jtulach@1348
|
188 |
* If requireEnd is true, and a match was found, then more
|
jtulach@1348
|
189 |
* input could cause the match to be lost.
|
jtulach@1348
|
190 |
* If requireEnd is false and a match was found, then more
|
jtulach@1348
|
191 |
* input might change the match but the match won't be lost.
|
jtulach@1348
|
192 |
* If a match was not found, then requireEnd has no meaning.
|
jtulach@1348
|
193 |
*/
|
jtulach@1348
|
194 |
boolean requireEnd;
|
jtulach@1348
|
195 |
|
jtulach@1348
|
196 |
/**
|
jtulach@1348
|
197 |
* If transparentBounds is true then the boundaries of this
|
jtulach@1348
|
198 |
* matcher's region are transparent to lookahead, lookbehind,
|
jtulach@1348
|
199 |
* and boundary matching constructs that try to see beyond them.
|
jtulach@1348
|
200 |
*/
|
jtulach@1348
|
201 |
boolean transparentBounds = false;
|
jtulach@1348
|
202 |
|
jtulach@1348
|
203 |
/**
|
jtulach@1348
|
204 |
* If anchoringBounds is true then the boundaries of this
|
jtulach@1348
|
205 |
* matcher's region match anchors such as ^ and $.
|
jtulach@1348
|
206 |
*/
|
jtulach@1348
|
207 |
boolean anchoringBounds = true;
|
jtulach@1348
|
208 |
|
jtulach@1348
|
209 |
/**
|
jtulach@1348
|
210 |
* No default constructor.
|
jtulach@1348
|
211 |
*/
|
jtulach@1348
|
212 |
Matcher() {
|
jtulach@1348
|
213 |
}
|
jtulach@1348
|
214 |
|
jtulach@1348
|
215 |
/**
|
jtulach@1348
|
216 |
* All matchers have the state used by Pattern during a match.
|
jtulach@1348
|
217 |
*/
|
jtulach@1348
|
218 |
Matcher(Pattern parent, CharSequence text) {
|
jtulach@1348
|
219 |
this.parentPattern = parent;
|
jtulach@1348
|
220 |
this.text = text;
|
jtulach@1348
|
221 |
|
jtulach@1348
|
222 |
// Allocate state storage
|
jtulach@1348
|
223 |
int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
|
jtulach@1348
|
224 |
groups = new int[parentGroupCount * 2];
|
jtulach@1348
|
225 |
locals = new int[parent.localCount];
|
jtulach@1348
|
226 |
|
jtulach@1348
|
227 |
// Put fields into initial states
|
jtulach@1348
|
228 |
reset();
|
jtulach@1348
|
229 |
}
|
jtulach@1348
|
230 |
|
jtulach@1348
|
231 |
/**
|
jtulach@1348
|
232 |
* Returns the pattern that is interpreted by this matcher.
|
jtulach@1348
|
233 |
*
|
jtulach@1348
|
234 |
* @return The pattern for which this matcher was created
|
jtulach@1348
|
235 |
*/
|
jtulach@1348
|
236 |
public Pattern pattern() {
|
jtulach@1348
|
237 |
return parentPattern;
|
jtulach@1348
|
238 |
}
|
jtulach@1348
|
239 |
|
jtulach@1348
|
240 |
/**
|
jtulach@1348
|
241 |
* Returns the match state of this matcher as a {@link MatchResult}.
|
jtulach@1348
|
242 |
* The result is unaffected by subsequent operations performed upon this
|
jtulach@1348
|
243 |
* matcher.
|
jtulach@1348
|
244 |
*
|
jtulach@1348
|
245 |
* @return a <code>MatchResult</code> with the state of this matcher
|
jtulach@1348
|
246 |
* @since 1.5
|
jtulach@1348
|
247 |
*/
|
jtulach@1348
|
248 |
public MatchResult toMatchResult() {
|
jtulach@1348
|
249 |
Matcher result = new Matcher(this.parentPattern, text.toString());
|
jtulach@1348
|
250 |
result.first = this.first;
|
jtulach@1348
|
251 |
result.last = this.last;
|
jtulach@1348
|
252 |
result.groups = this.groups.clone();
|
jtulach@1348
|
253 |
return result;
|
jtulach@1348
|
254 |
}
|
jtulach@1348
|
255 |
|
jtulach@1348
|
256 |
/**
|
jtulach@1348
|
257 |
* Changes the <tt>Pattern</tt> that this <tt>Matcher</tt> uses to
|
jtulach@1348
|
258 |
* find matches with.
|
jtulach@1348
|
259 |
*
|
jtulach@1348
|
260 |
* <p> This method causes this matcher to lose information
|
jtulach@1348
|
261 |
* about the groups of the last match that occurred. The
|
jtulach@1348
|
262 |
* matcher's position in the input is maintained and its
|
jtulach@1348
|
263 |
* last append position is unaffected.</p>
|
jtulach@1348
|
264 |
*
|
jtulach@1348
|
265 |
* @param newPattern
|
jtulach@1348
|
266 |
* The new pattern used by this matcher
|
jtulach@1348
|
267 |
* @return This matcher
|
jtulach@1348
|
268 |
* @throws IllegalArgumentException
|
jtulach@1348
|
269 |
* If newPattern is <tt>null</tt>
|
jtulach@1348
|
270 |
* @since 1.5
|
jtulach@1348
|
271 |
*/
|
jtulach@1348
|
272 |
public Matcher usePattern(Pattern newPattern) {
|
jtulach@1348
|
273 |
if (newPattern == null)
|
jtulach@1348
|
274 |
throw new IllegalArgumentException("Pattern cannot be null");
|
jtulach@1348
|
275 |
parentPattern = newPattern;
|
jtulach@1348
|
276 |
|
jtulach@1348
|
277 |
// Reallocate state storage
|
jtulach@1348
|
278 |
int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
|
jtulach@1348
|
279 |
groups = new int[parentGroupCount * 2];
|
jtulach@1348
|
280 |
locals = new int[newPattern.localCount];
|
jtulach@1348
|
281 |
for (int i = 0; i < groups.length; i++)
|
jtulach@1348
|
282 |
groups[i] = -1;
|
jtulach@1348
|
283 |
for (int i = 0; i < locals.length; i++)
|
jtulach@1348
|
284 |
locals[i] = -1;
|
jtulach@1348
|
285 |
return this;
|
jtulach@1348
|
286 |
}
|
jtulach@1348
|
287 |
|
jtulach@1348
|
288 |
/**
|
jtulach@1348
|
289 |
* Resets this matcher.
|
jtulach@1348
|
290 |
*
|
jtulach@1348
|
291 |
* <p> Resetting a matcher discards all of its explicit state information
|
jtulach@1348
|
292 |
* and sets its append position to zero. The matcher's region is set to the
|
jtulach@1348
|
293 |
* default region, which is its entire character sequence. The anchoring
|
jtulach@1348
|
294 |
* and transparency of this matcher's region boundaries are unaffected.
|
jtulach@1348
|
295 |
*
|
jtulach@1348
|
296 |
* @return This matcher
|
jtulach@1348
|
297 |
*/
|
jtulach@1348
|
298 |
public Matcher reset() {
|
jtulach@1348
|
299 |
first = -1;
|
jtulach@1348
|
300 |
last = 0;
|
jtulach@1348
|
301 |
oldLast = -1;
|
jtulach@1348
|
302 |
for(int i=0; i<groups.length; i++)
|
jtulach@1348
|
303 |
groups[i] = -1;
|
jtulach@1348
|
304 |
for(int i=0; i<locals.length; i++)
|
jtulach@1348
|
305 |
locals[i] = -1;
|
jtulach@1348
|
306 |
lastAppendPosition = 0;
|
jtulach@1348
|
307 |
from = 0;
|
jtulach@1348
|
308 |
to = getTextLength();
|
jtulach@1348
|
309 |
return this;
|
jtulach@1348
|
310 |
}
|
jtulach@1348
|
311 |
|
jtulach@1348
|
312 |
/**
|
jtulach@1348
|
313 |
* Resets this matcher with a new input sequence.
|
jtulach@1348
|
314 |
*
|
jtulach@1348
|
315 |
* <p> Resetting a matcher discards all of its explicit state information
|
jtulach@1348
|
316 |
* and sets its append position to zero. The matcher's region is set to
|
jtulach@1348
|
317 |
* the default region, which is its entire character sequence. The
|
jtulach@1348
|
318 |
* anchoring and transparency of this matcher's region boundaries are
|
jtulach@1348
|
319 |
* unaffected.
|
jtulach@1348
|
320 |
*
|
jtulach@1348
|
321 |
* @param input
|
jtulach@1348
|
322 |
* The new input character sequence
|
jtulach@1348
|
323 |
*
|
jtulach@1348
|
324 |
* @return This matcher
|
jtulach@1348
|
325 |
*/
|
jtulach@1348
|
326 |
public Matcher reset(CharSequence input) {
|
jtulach@1348
|
327 |
text = input;
|
jtulach@1348
|
328 |
return reset();
|
jtulach@1348
|
329 |
}
|
jtulach@1348
|
330 |
|
jtulach@1348
|
331 |
/**
|
jtulach@1348
|
332 |
* Returns the start index of the previous match. </p>
|
jtulach@1348
|
333 |
*
|
jtulach@1348
|
334 |
* @return The index of the first character matched
|
jtulach@1348
|
335 |
*
|
jtulach@1348
|
336 |
* @throws IllegalStateException
|
jtulach@1348
|
337 |
* If no match has yet been attempted,
|
jtulach@1348
|
338 |
* or if the previous match operation failed
|
jtulach@1348
|
339 |
*/
|
jtulach@1348
|
340 |
public int start() {
|
jtulach@1348
|
341 |
if (first < 0)
|
jtulach@1348
|
342 |
throw new IllegalStateException("No match available");
|
jtulach@1348
|
343 |
return first;
|
jtulach@1348
|
344 |
}
|
jtulach@1348
|
345 |
|
jtulach@1348
|
346 |
/**
|
jtulach@1348
|
347 |
* Returns the start index of the subsequence captured by the given group
|
jtulach@1348
|
348 |
* during the previous match operation.
|
jtulach@1348
|
349 |
*
|
jtulach@1348
|
350 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
|
jtulach@1348
|
351 |
* to right, starting at one. Group zero denotes the entire pattern, so
|
jtulach@1348
|
352 |
* the expression <i>m.</i><tt>start(0)</tt> is equivalent to
|
jtulach@1348
|
353 |
* <i>m.</i><tt>start()</tt>. </p>
|
jtulach@1348
|
354 |
*
|
jtulach@1348
|
355 |
* @param group
|
jtulach@1348
|
356 |
* The index of a capturing group in this matcher's pattern
|
jtulach@1348
|
357 |
*
|
jtulach@1348
|
358 |
* @return The index of the first character captured by the group,
|
jtulach@1348
|
359 |
* or <tt>-1</tt> if the match was successful but the group
|
jtulach@1348
|
360 |
* itself did not match anything
|
jtulach@1348
|
361 |
*
|
jtulach@1348
|
362 |
* @throws IllegalStateException
|
jtulach@1348
|
363 |
* If no match has yet been attempted,
|
jtulach@1348
|
364 |
* or if the previous match operation failed
|
jtulach@1348
|
365 |
*
|
jtulach@1348
|
366 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
367 |
* If there is no capturing group in the pattern
|
jtulach@1348
|
368 |
* with the given index
|
jtulach@1348
|
369 |
*/
|
jtulach@1348
|
370 |
public int start(int group) {
|
jtulach@1348
|
371 |
if (first < 0)
|
jtulach@1348
|
372 |
throw new IllegalStateException("No match available");
|
jtulach@1348
|
373 |
if (group > groupCount())
|
jtulach@1348
|
374 |
throw new IndexOutOfBoundsException("No group " + group);
|
jtulach@1348
|
375 |
return groups[group * 2];
|
jtulach@1348
|
376 |
}
|
jtulach@1348
|
377 |
|
jtulach@1348
|
378 |
/**
|
jtulach@1348
|
379 |
* Returns the offset after the last character matched. </p>
|
jtulach@1348
|
380 |
*
|
jtulach@1348
|
381 |
* @return The offset after the last character matched
|
jtulach@1348
|
382 |
*
|
jtulach@1348
|
383 |
* @throws IllegalStateException
|
jtulach@1348
|
384 |
* If no match has yet been attempted,
|
jtulach@1348
|
385 |
* or if the previous match operation failed
|
jtulach@1348
|
386 |
*/
|
jtulach@1348
|
387 |
public int end() {
|
jtulach@1348
|
388 |
if (first < 0)
|
jtulach@1348
|
389 |
throw new IllegalStateException("No match available");
|
jtulach@1348
|
390 |
return last;
|
jtulach@1348
|
391 |
}
|
jtulach@1348
|
392 |
|
jtulach@1348
|
393 |
/**
|
jtulach@1348
|
394 |
* Returns the offset after the last character of the subsequence
|
jtulach@1348
|
395 |
* captured by the given group during the previous match operation.
|
jtulach@1348
|
396 |
*
|
jtulach@1348
|
397 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
|
jtulach@1348
|
398 |
* to right, starting at one. Group zero denotes the entire pattern, so
|
jtulach@1348
|
399 |
* the expression <i>m.</i><tt>end(0)</tt> is equivalent to
|
jtulach@1348
|
400 |
* <i>m.</i><tt>end()</tt>. </p>
|
jtulach@1348
|
401 |
*
|
jtulach@1348
|
402 |
* @param group
|
jtulach@1348
|
403 |
* The index of a capturing group in this matcher's pattern
|
jtulach@1348
|
404 |
*
|
jtulach@1348
|
405 |
* @return The offset after the last character captured by the group,
|
jtulach@1348
|
406 |
* or <tt>-1</tt> if the match was successful
|
jtulach@1348
|
407 |
* but the group itself did not match anything
|
jtulach@1348
|
408 |
*
|
jtulach@1348
|
409 |
* @throws IllegalStateException
|
jtulach@1348
|
410 |
* If no match has yet been attempted,
|
jtulach@1348
|
411 |
* or if the previous match operation failed
|
jtulach@1348
|
412 |
*
|
jtulach@1348
|
413 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
414 |
* If there is no capturing group in the pattern
|
jtulach@1348
|
415 |
* with the given index
|
jtulach@1348
|
416 |
*/
|
jtulach@1348
|
417 |
public int end(int group) {
|
jtulach@1348
|
418 |
if (first < 0)
|
jtulach@1348
|
419 |
throw new IllegalStateException("No match available");
|
jtulach@1348
|
420 |
if (group > groupCount())
|
jtulach@1348
|
421 |
throw new IndexOutOfBoundsException("No group " + group);
|
jtulach@1348
|
422 |
return groups[group * 2 + 1];
|
jtulach@1348
|
423 |
}
|
jtulach@1348
|
424 |
|
jtulach@1348
|
425 |
/**
|
jtulach@1348
|
426 |
* Returns the input subsequence matched by the previous match.
|
jtulach@1348
|
427 |
*
|
jtulach@1348
|
428 |
* <p> For a matcher <i>m</i> with input sequence <i>s</i>,
|
jtulach@1348
|
429 |
* the expressions <i>m.</i><tt>group()</tt> and
|
jtulach@1348
|
430 |
* <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt> <i>m.</i><tt>end())</tt>
|
jtulach@1348
|
431 |
* are equivalent. </p>
|
jtulach@1348
|
432 |
*
|
jtulach@1348
|
433 |
* <p> Note that some patterns, for example <tt>a*</tt>, match the empty
|
jtulach@1348
|
434 |
* string. This method will return the empty string when the pattern
|
jtulach@1348
|
435 |
* successfully matches the empty string in the input. </p>
|
jtulach@1348
|
436 |
*
|
jtulach@1348
|
437 |
* @return The (possibly empty) subsequence matched by the previous match,
|
jtulach@1348
|
438 |
* in string form
|
jtulach@1348
|
439 |
*
|
jtulach@1348
|
440 |
* @throws IllegalStateException
|
jtulach@1348
|
441 |
* If no match has yet been attempted,
|
jtulach@1348
|
442 |
* or if the previous match operation failed
|
jtulach@1348
|
443 |
*/
|
jtulach@1348
|
444 |
public String group() {
|
jtulach@1348
|
445 |
return group(0);
|
jtulach@1348
|
446 |
}
|
jtulach@1348
|
447 |
|
jtulach@1348
|
448 |
/**
|
jtulach@1348
|
449 |
* Returns the input subsequence captured by the given group during the
|
jtulach@1348
|
450 |
* previous match operation.
|
jtulach@1348
|
451 |
*
|
jtulach@1348
|
452 |
* <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
|
jtulach@1348
|
453 |
* <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
|
jtulach@1348
|
454 |
* <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>
|
jtulach@1348
|
455 |
* are equivalent. </p>
|
jtulach@1348
|
456 |
*
|
jtulach@1348
|
457 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
|
jtulach@1348
|
458 |
* to right, starting at one. Group zero denotes the entire pattern, so
|
jtulach@1348
|
459 |
* the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
|
jtulach@1348
|
460 |
* </p>
|
jtulach@1348
|
461 |
*
|
jtulach@1348
|
462 |
* <p> If the match was successful but the group specified failed to match
|
jtulach@1348
|
463 |
* any part of the input sequence, then <tt>null</tt> is returned. Note
|
jtulach@1348
|
464 |
* that some groups, for example <tt>(a*)</tt>, match the empty string.
|
jtulach@1348
|
465 |
* This method will return the empty string when such a group successfully
|
jtulach@1348
|
466 |
* matches the empty string in the input. </p>
|
jtulach@1348
|
467 |
*
|
jtulach@1348
|
468 |
* @param group
|
jtulach@1348
|
469 |
* The index of a capturing group in this matcher's pattern
|
jtulach@1348
|
470 |
*
|
jtulach@1348
|
471 |
* @return The (possibly empty) subsequence captured by the group
|
jtulach@1348
|
472 |
* during the previous match, or <tt>null</tt> if the group
|
jtulach@1348
|
473 |
* failed to match part of the input
|
jtulach@1348
|
474 |
*
|
jtulach@1348
|
475 |
* @throws IllegalStateException
|
jtulach@1348
|
476 |
* If no match has yet been attempted,
|
jtulach@1348
|
477 |
* or if the previous match operation failed
|
jtulach@1348
|
478 |
*
|
jtulach@1348
|
479 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
480 |
* If there is no capturing group in the pattern
|
jtulach@1348
|
481 |
* with the given index
|
jtulach@1348
|
482 |
*/
|
jtulach@1348
|
483 |
public String group(int group) {
|
jtulach@1348
|
484 |
if (first < 0)
|
jtulach@1348
|
485 |
throw new IllegalStateException("No match found");
|
jtulach@1348
|
486 |
if (group < 0 || group > groupCount())
|
jtulach@1348
|
487 |
throw new IndexOutOfBoundsException("No group " + group);
|
jtulach@1348
|
488 |
if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
|
jtulach@1348
|
489 |
return null;
|
jtulach@1348
|
490 |
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
|
jtulach@1348
|
491 |
}
|
jtulach@1348
|
492 |
|
jtulach@1348
|
493 |
/**
|
jtulach@1348
|
494 |
* Returns the input subsequence captured by the given
|
jtulach@1348
|
495 |
* <a href="Pattern.html#groupname">named-capturing group</a> during the previous
|
jtulach@1348
|
496 |
* match operation.
|
jtulach@1348
|
497 |
*
|
jtulach@1348
|
498 |
* <p> If the match was successful but the group specified failed to match
|
jtulach@1348
|
499 |
* any part of the input sequence, then <tt>null</tt> is returned. Note
|
jtulach@1348
|
500 |
* that some groups, for example <tt>(a*)</tt>, match the empty string.
|
jtulach@1348
|
501 |
* This method will return the empty string when such a group successfully
|
jtulach@1348
|
502 |
* matches the empty string in the input. </p>
|
jtulach@1348
|
503 |
*
|
jtulach@1348
|
504 |
* @param name
|
jtulach@1348
|
505 |
* The name of a named-capturing group in this matcher's pattern
|
jtulach@1348
|
506 |
*
|
jtulach@1348
|
507 |
* @return The (possibly empty) subsequence captured by the named group
|
jtulach@1348
|
508 |
* during the previous match, or <tt>null</tt> if the group
|
jtulach@1348
|
509 |
* failed to match part of the input
|
jtulach@1348
|
510 |
*
|
jtulach@1348
|
511 |
* @throws IllegalStateException
|
jtulach@1348
|
512 |
* If no match has yet been attempted,
|
jtulach@1348
|
513 |
* or if the previous match operation failed
|
jtulach@1348
|
514 |
*
|
jtulach@1348
|
515 |
* @throws IllegalArgumentException
|
jtulach@1348
|
516 |
* If there is no capturing group in the pattern
|
jtulach@1348
|
517 |
* with the given name
|
jtulach@1348
|
518 |
*/
|
jtulach@1348
|
519 |
public String group(String name) {
|
jtulach@1348
|
520 |
if (name == null)
|
jtulach@1348
|
521 |
throw new NullPointerException("Null group name");
|
jtulach@1348
|
522 |
if (first < 0)
|
jtulach@1348
|
523 |
throw new IllegalStateException("No match found");
|
jtulach@1348
|
524 |
if (!parentPattern.namedGroups().containsKey(name))
|
jtulach@1348
|
525 |
throw new IllegalArgumentException("No group with name <" + name + ">");
|
jtulach@1348
|
526 |
int group = parentPattern.namedGroups().get(name);
|
jtulach@1348
|
527 |
if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
|
jtulach@1348
|
528 |
return null;
|
jtulach@1348
|
529 |
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
|
jtulach@1348
|
530 |
}
|
jtulach@1348
|
531 |
|
jtulach@1348
|
532 |
/**
|
jtulach@1348
|
533 |
* Returns the number of capturing groups in this matcher's pattern.
|
jtulach@1348
|
534 |
*
|
jtulach@1348
|
535 |
* <p> Group zero denotes the entire pattern by convention. It is not
|
jtulach@1348
|
536 |
* included in this count.
|
jtulach@1348
|
537 |
*
|
jtulach@1348
|
538 |
* <p> Any non-negative integer smaller than or equal to the value
|
jtulach@1348
|
539 |
* returned by this method is guaranteed to be a valid group index for
|
jtulach@1348
|
540 |
* this matcher. </p>
|
jtulach@1348
|
541 |
*
|
jtulach@1348
|
542 |
* @return The number of capturing groups in this matcher's pattern
|
jtulach@1348
|
543 |
*/
|
jtulach@1348
|
544 |
public int groupCount() {
|
jtulach@1348
|
545 |
return parentPattern.capturingGroupCount - 1;
|
jtulach@1348
|
546 |
}
|
jtulach@1348
|
547 |
|
jtulach@1348
|
548 |
/**
|
jtulach@1348
|
549 |
* Attempts to match the entire region against the pattern.
|
jtulach@1348
|
550 |
*
|
jtulach@1348
|
551 |
* <p> If the match succeeds then more information can be obtained via the
|
jtulach@1348
|
552 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p>
|
jtulach@1348
|
553 |
*
|
jtulach@1348
|
554 |
* @return <tt>true</tt> if, and only if, the entire region sequence
|
jtulach@1348
|
555 |
* matches this matcher's pattern
|
jtulach@1348
|
556 |
*/
|
jtulach@1348
|
557 |
public boolean matches() {
|
jtulach@1348
|
558 |
return match(from, ENDANCHOR);
|
jtulach@1348
|
559 |
}
|
jtulach@1348
|
560 |
|
jtulach@1348
|
561 |
/**
|
jtulach@1348
|
562 |
* Attempts to find the next subsequence of the input sequence that matches
|
jtulach@1348
|
563 |
* the pattern.
|
jtulach@1348
|
564 |
*
|
jtulach@1348
|
565 |
* <p> This method starts at the beginning of this matcher's region, or, if
|
jtulach@1348
|
566 |
* a previous invocation of the method was successful and the matcher has
|
jtulach@1348
|
567 |
* not since been reset, at the first character not matched by the previous
|
jtulach@1348
|
568 |
* match.
|
jtulach@1348
|
569 |
*
|
jtulach@1348
|
570 |
* <p> If the match succeeds then more information can be obtained via the
|
jtulach@1348
|
571 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p>
|
jtulach@1348
|
572 |
*
|
jtulach@1348
|
573 |
* @return <tt>true</tt> if, and only if, a subsequence of the input
|
jtulach@1348
|
574 |
* sequence matches this matcher's pattern
|
jtulach@1348
|
575 |
*/
|
jtulach@1348
|
576 |
public boolean find() {
|
jtulach@1348
|
577 |
int nextSearchIndex = last;
|
jtulach@1348
|
578 |
if (nextSearchIndex == first)
|
jtulach@1348
|
579 |
nextSearchIndex++;
|
jtulach@1348
|
580 |
|
jtulach@1348
|
581 |
// If next search starts before region, start it at region
|
jtulach@1348
|
582 |
if (nextSearchIndex < from)
|
jtulach@1348
|
583 |
nextSearchIndex = from;
|
jtulach@1348
|
584 |
|
jtulach@1348
|
585 |
// If next search starts beyond region then it fails
|
jtulach@1348
|
586 |
if (nextSearchIndex > to) {
|
jtulach@1348
|
587 |
for (int i = 0; i < groups.length; i++)
|
jtulach@1348
|
588 |
groups[i] = -1;
|
jtulach@1348
|
589 |
return false;
|
jtulach@1348
|
590 |
}
|
jtulach@1348
|
591 |
return search(nextSearchIndex);
|
jtulach@1348
|
592 |
}
|
jtulach@1348
|
593 |
|
jtulach@1348
|
594 |
/**
|
jtulach@1348
|
595 |
* Resets this matcher and then attempts to find the next subsequence of
|
jtulach@1348
|
596 |
* the input sequence that matches the pattern, starting at the specified
|
jtulach@1348
|
597 |
* index.
|
jtulach@1348
|
598 |
*
|
jtulach@1348
|
599 |
* <p> If the match succeeds then more information can be obtained via the
|
jtulach@1348
|
600 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent
|
jtulach@1348
|
601 |
* invocations of the {@link #find()} method will start at the first
|
jtulach@1348
|
602 |
* character not matched by this match. </p>
|
jtulach@1348
|
603 |
*
|
jtulach@1348
|
604 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
605 |
* If start is less than zero or if start is greater than the
|
jtulach@1348
|
606 |
* length of the input sequence.
|
jtulach@1348
|
607 |
*
|
jtulach@1348
|
608 |
* @return <tt>true</tt> if, and only if, a subsequence of the input
|
jtulach@1348
|
609 |
* sequence starting at the given index matches this matcher's
|
jtulach@1348
|
610 |
* pattern
|
jtulach@1348
|
611 |
*/
|
jtulach@1348
|
612 |
public boolean find(int start) {
|
jtulach@1348
|
613 |
int limit = getTextLength();
|
jtulach@1348
|
614 |
if ((start < 0) || (start > limit))
|
jtulach@1348
|
615 |
throw new IndexOutOfBoundsException("Illegal start index");
|
jtulach@1348
|
616 |
reset();
|
jtulach@1348
|
617 |
return search(start);
|
jtulach@1348
|
618 |
}
|
jtulach@1348
|
619 |
|
jtulach@1348
|
620 |
/**
|
jtulach@1348
|
621 |
* Attempts to match the input sequence, starting at the beginning of the
|
jtulach@1348
|
622 |
* region, against the pattern.
|
jtulach@1348
|
623 |
*
|
jtulach@1348
|
624 |
* <p> Like the {@link #matches matches} method, this method always starts
|
jtulach@1348
|
625 |
* at the beginning of the region; unlike that method, it does not
|
jtulach@1348
|
626 |
* require that the entire region be matched.
|
jtulach@1348
|
627 |
*
|
jtulach@1348
|
628 |
* <p> If the match succeeds then more information can be obtained via the
|
jtulach@1348
|
629 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p>
|
jtulach@1348
|
630 |
*
|
jtulach@1348
|
631 |
* @return <tt>true</tt> if, and only if, a prefix of the input
|
jtulach@1348
|
632 |
* sequence matches this matcher's pattern
|
jtulach@1348
|
633 |
*/
|
jtulach@1348
|
634 |
public boolean lookingAt() {
|
jtulach@1348
|
635 |
return match(from, NOANCHOR);
|
jtulach@1348
|
636 |
}
|
jtulach@1348
|
637 |
|
jtulach@1348
|
638 |
/**
|
jtulach@1348
|
639 |
* Returns a literal replacement <code>String</code> for the specified
|
jtulach@1348
|
640 |
* <code>String</code>.
|
jtulach@1348
|
641 |
*
|
jtulach@1348
|
642 |
* This method produces a <code>String</code> that will work
|
jtulach@1348
|
643 |
* as a literal replacement <code>s</code> in the
|
jtulach@1348
|
644 |
* <code>appendReplacement</code> method of the {@link Matcher} class.
|
jtulach@1348
|
645 |
* The <code>String</code> produced will match the sequence of characters
|
jtulach@1348
|
646 |
* in <code>s</code> treated as a literal sequence. Slashes ('\') and
|
jtulach@1348
|
647 |
* dollar signs ('$') will be given no special meaning.
|
jtulach@1348
|
648 |
*
|
jtulach@1348
|
649 |
* @param s The string to be literalized
|
jtulach@1348
|
650 |
* @return A literal string replacement
|
jtulach@1348
|
651 |
* @since 1.5
|
jtulach@1348
|
652 |
*/
|
jtulach@1348
|
653 |
public static String quoteReplacement(String s) {
|
jtulach@1348
|
654 |
if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
|
jtulach@1348
|
655 |
return s;
|
jtulach@1348
|
656 |
StringBuilder sb = new StringBuilder();
|
jtulach@1348
|
657 |
for (int i=0; i<s.length(); i++) {
|
jtulach@1348
|
658 |
char c = s.charAt(i);
|
jtulach@1348
|
659 |
if (c == '\\' || c == '$') {
|
jtulach@1348
|
660 |
sb.append('\\');
|
jtulach@1348
|
661 |
}
|
jtulach@1348
|
662 |
sb.append(c);
|
jtulach@1348
|
663 |
}
|
jtulach@1348
|
664 |
return sb.toString();
|
jtulach@1348
|
665 |
}
|
jtulach@1348
|
666 |
|
jtulach@1348
|
667 |
/**
|
jtulach@1348
|
668 |
* Implements a non-terminal append-and-replace step.
|
jtulach@1348
|
669 |
*
|
jtulach@1348
|
670 |
* <p> This method performs the following actions: </p>
|
jtulach@1348
|
671 |
*
|
jtulach@1348
|
672 |
* <ol>
|
jtulach@1348
|
673 |
*
|
jtulach@1348
|
674 |
* <li><p> It reads characters from the input sequence, starting at the
|
jtulach@1348
|
675 |
* append position, and appends them to the given string buffer. It
|
jtulach@1348
|
676 |
* stops after reading the last character preceding the previous match,
|
jtulach@1348
|
677 |
* that is, the character at index {@link
|
jtulach@1348
|
678 |
* #start()} <tt>-</tt> <tt>1</tt>. </p></li>
|
jtulach@1348
|
679 |
*
|
jtulach@1348
|
680 |
* <li><p> It appends the given replacement string to the string buffer.
|
jtulach@1348
|
681 |
* </p></li>
|
jtulach@1348
|
682 |
*
|
jtulach@1348
|
683 |
* <li><p> It sets the append position of this matcher to the index of
|
jtulach@1348
|
684 |
* the last character matched, plus one, that is, to {@link #end()}.
|
jtulach@1348
|
685 |
* </p></li>
|
jtulach@1348
|
686 |
*
|
jtulach@1348
|
687 |
* </ol>
|
jtulach@1348
|
688 |
*
|
jtulach@1348
|
689 |
* <p> The replacement string may contain references to subsequences
|
jtulach@1348
|
690 |
* captured during the previous match: Each occurrence of
|
jtulach@1348
|
691 |
* <tt>${</tt><i>name</i><tt>}</tt> or <tt>$</tt><i>g</i>
|
jtulach@1348
|
692 |
* will be replaced by the result of evaluating the corresponding
|
jtulach@1348
|
693 |
* {@link #group(String) group(name)} or {@link #group(int) group(g)</tt>}
|
jtulach@1348
|
694 |
* respectively. For <tt>$</tt><i>g</i><tt></tt>,
|
jtulach@1348
|
695 |
* the first number after the <tt>$</tt> is always treated as part of
|
jtulach@1348
|
696 |
* the group reference. Subsequent numbers are incorporated into g if
|
jtulach@1348
|
697 |
* they would form a legal group reference. Only the numerals '0'
|
jtulach@1348
|
698 |
* through '9' are considered as potential components of the group
|
jtulach@1348
|
699 |
* reference. If the second group matched the string <tt>"foo"</tt>, for
|
jtulach@1348
|
700 |
* example, then passing the replacement string <tt>"$2bar"</tt> would
|
jtulach@1348
|
701 |
* cause <tt>"foobar"</tt> to be appended to the string buffer. A dollar
|
jtulach@1348
|
702 |
* sign (<tt>$</tt>) may be included as a literal in the replacement
|
jtulach@1348
|
703 |
* string by preceding it with a backslash (<tt>\$</tt>).
|
jtulach@1348
|
704 |
*
|
jtulach@1348
|
705 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
|
jtulach@1348
|
706 |
* the replacement string may cause the results to be different than if it
|
jtulach@1348
|
707 |
* were being treated as a literal replacement string. Dollar signs may be
|
jtulach@1348
|
708 |
* treated as references to captured subsequences as described above, and
|
jtulach@1348
|
709 |
* backslashes are used to escape literal characters in the replacement
|
jtulach@1348
|
710 |
* string.
|
jtulach@1348
|
711 |
*
|
jtulach@1348
|
712 |
* <p> This method is intended to be used in a loop together with the
|
jtulach@1348
|
713 |
* {@link #appendTail appendTail} and {@link #find find} methods. The
|
jtulach@1348
|
714 |
* following code, for example, writes <tt>one dog two dogs in the
|
jtulach@1348
|
715 |
* yard</tt> to the standard-output stream: </p>
|
jtulach@1348
|
716 |
*
|
jtulach@1348
|
717 |
* <blockquote><pre>
|
jtulach@1348
|
718 |
* Pattern p = Pattern.compile("cat");
|
jtulach@1348
|
719 |
* Matcher m = p.matcher("one cat two cats in the yard");
|
jtulach@1348
|
720 |
* StringBuffer sb = new StringBuffer();
|
jtulach@1348
|
721 |
* while (m.find()) {
|
jtulach@1348
|
722 |
* m.appendReplacement(sb, "dog");
|
jtulach@1348
|
723 |
* }
|
jtulach@1348
|
724 |
* m.appendTail(sb);
|
jtulach@1348
|
725 |
* System.out.println(sb.toString());</pre></blockquote>
|
jtulach@1348
|
726 |
*
|
jtulach@1348
|
727 |
* @param sb
|
jtulach@1348
|
728 |
* The target string buffer
|
jtulach@1348
|
729 |
*
|
jtulach@1348
|
730 |
* @param replacement
|
jtulach@1348
|
731 |
* The replacement string
|
jtulach@1348
|
732 |
*
|
jtulach@1348
|
733 |
* @return This matcher
|
jtulach@1348
|
734 |
*
|
jtulach@1348
|
735 |
* @throws IllegalStateException
|
jtulach@1348
|
736 |
* If no match has yet been attempted,
|
jtulach@1348
|
737 |
* or if the previous match operation failed
|
jtulach@1348
|
738 |
*
|
jtulach@1348
|
739 |
* @throws IllegalArgumentException
|
jtulach@1348
|
740 |
* If the replacement string refers to a named-capturing
|
jtulach@1348
|
741 |
* group that does not exist in the pattern
|
jtulach@1348
|
742 |
*
|
jtulach@1348
|
743 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
744 |
* If the replacement string refers to a capturing group
|
jtulach@1348
|
745 |
* that does not exist in the pattern
|
jtulach@1348
|
746 |
*/
|
jtulach@1348
|
747 |
public Matcher appendReplacement(StringBuffer sb, String replacement) {
|
jtulach@1348
|
748 |
|
jtulach@1348
|
749 |
// If no match, return error
|
jtulach@1348
|
750 |
if (first < 0)
|
jtulach@1348
|
751 |
throw new IllegalStateException("No match available");
|
jtulach@1348
|
752 |
|
jtulach@1348
|
753 |
// Process substitution string to replace group references with groups
|
jtulach@1348
|
754 |
int cursor = 0;
|
jtulach@1348
|
755 |
StringBuilder result = new StringBuilder();
|
jtulach@1348
|
756 |
|
jtulach@1348
|
757 |
while (cursor < replacement.length()) {
|
jtulach@1348
|
758 |
char nextChar = replacement.charAt(cursor);
|
jtulach@1348
|
759 |
if (nextChar == '\\') {
|
jtulach@1348
|
760 |
cursor++;
|
jtulach@1348
|
761 |
nextChar = replacement.charAt(cursor);
|
jtulach@1348
|
762 |
result.append(nextChar);
|
jtulach@1348
|
763 |
cursor++;
|
jtulach@1348
|
764 |
} else if (nextChar == '$') {
|
jtulach@1348
|
765 |
// Skip past $
|
jtulach@1348
|
766 |
cursor++;
|
jtulach@1348
|
767 |
// A StringIndexOutOfBoundsException is thrown if
|
jtulach@1348
|
768 |
// this "$" is the last character in replacement
|
jtulach@1348
|
769 |
// string in current implementation, a IAE might be
|
jtulach@1348
|
770 |
// more appropriate.
|
jtulach@1348
|
771 |
nextChar = replacement.charAt(cursor);
|
jtulach@1348
|
772 |
int refNum = -1;
|
jtulach@1348
|
773 |
if (nextChar == '{') {
|
jtulach@1348
|
774 |
cursor++;
|
jtulach@1348
|
775 |
StringBuilder gsb = new StringBuilder();
|
jtulach@1348
|
776 |
while (cursor < replacement.length()) {
|
jtulach@1348
|
777 |
nextChar = replacement.charAt(cursor);
|
jtulach@1348
|
778 |
if (ASCII.isLower(nextChar) ||
|
jtulach@1348
|
779 |
ASCII.isUpper(nextChar) ||
|
jtulach@1348
|
780 |
ASCII.isDigit(nextChar)) {
|
jtulach@1348
|
781 |
gsb.append(nextChar);
|
jtulach@1348
|
782 |
cursor++;
|
jtulach@1348
|
783 |
} else {
|
jtulach@1348
|
784 |
break;
|
jtulach@1348
|
785 |
}
|
jtulach@1348
|
786 |
}
|
jtulach@1348
|
787 |
if (gsb.length() == 0)
|
jtulach@1348
|
788 |
throw new IllegalArgumentException(
|
jtulach@1348
|
789 |
"named capturing group has 0 length name");
|
jtulach@1348
|
790 |
if (nextChar != '}')
|
jtulach@1348
|
791 |
throw new IllegalArgumentException(
|
jtulach@1348
|
792 |
"named capturing group is missing trailing '}'");
|
jtulach@1348
|
793 |
String gname = gsb.toString();
|
jtulach@1348
|
794 |
if (ASCII.isDigit(gname.charAt(0)))
|
jtulach@1348
|
795 |
throw new IllegalArgumentException(
|
jtulach@1348
|
796 |
"capturing group name {" + gname +
|
jtulach@1348
|
797 |
"} starts with digit character");
|
jtulach@1348
|
798 |
if (!parentPattern.namedGroups().containsKey(gname))
|
jtulach@1348
|
799 |
throw new IllegalArgumentException(
|
jtulach@1348
|
800 |
"No group with name {" + gname + "}");
|
jtulach@1348
|
801 |
refNum = parentPattern.namedGroups().get(gname);
|
jtulach@1348
|
802 |
cursor++;
|
jtulach@1348
|
803 |
} else {
|
jtulach@1348
|
804 |
// The first number is always a group
|
jtulach@1348
|
805 |
refNum = (int)nextChar - '0';
|
jtulach@1348
|
806 |
if ((refNum < 0)||(refNum > 9))
|
jtulach@1348
|
807 |
throw new IllegalArgumentException(
|
jtulach@1348
|
808 |
"Illegal group reference");
|
jtulach@1348
|
809 |
cursor++;
|
jtulach@1348
|
810 |
// Capture the largest legal group string
|
jtulach@1348
|
811 |
boolean done = false;
|
jtulach@1348
|
812 |
while (!done) {
|
jtulach@1348
|
813 |
if (cursor >= replacement.length()) {
|
jtulach@1348
|
814 |
break;
|
jtulach@1348
|
815 |
}
|
jtulach@1348
|
816 |
int nextDigit = replacement.charAt(cursor) - '0';
|
jtulach@1348
|
817 |
if ((nextDigit < 0)||(nextDigit > 9)) { // not a number
|
jtulach@1348
|
818 |
break;
|
jtulach@1348
|
819 |
}
|
jtulach@1348
|
820 |
int newRefNum = (refNum * 10) + nextDigit;
|
jtulach@1348
|
821 |
if (groupCount() < newRefNum) {
|
jtulach@1348
|
822 |
done = true;
|
jtulach@1348
|
823 |
} else {
|
jtulach@1348
|
824 |
refNum = newRefNum;
|
jtulach@1348
|
825 |
cursor++;
|
jtulach@1348
|
826 |
}
|
jtulach@1348
|
827 |
}
|
jtulach@1348
|
828 |
}
|
jtulach@1348
|
829 |
// Append group
|
jtulach@1348
|
830 |
if (start(refNum) != -1 && end(refNum) != -1)
|
jtulach@1348
|
831 |
result.append(text, start(refNum), end(refNum));
|
jtulach@1348
|
832 |
} else {
|
jtulach@1348
|
833 |
result.append(nextChar);
|
jtulach@1348
|
834 |
cursor++;
|
jtulach@1348
|
835 |
}
|
jtulach@1348
|
836 |
}
|
jtulach@1348
|
837 |
// Append the intervening text
|
jtulach@1348
|
838 |
sb.append(text, lastAppendPosition, first);
|
jtulach@1348
|
839 |
// Append the match substitution
|
jtulach@1348
|
840 |
sb.append(result);
|
jtulach@1348
|
841 |
|
jtulach@1348
|
842 |
lastAppendPosition = last;
|
jtulach@1348
|
843 |
return this;
|
jtulach@1348
|
844 |
}
|
jtulach@1348
|
845 |
|
jtulach@1348
|
846 |
/**
|
jtulach@1348
|
847 |
* Implements a terminal append-and-replace step.
|
jtulach@1348
|
848 |
*
|
jtulach@1348
|
849 |
* <p> This method reads characters from the input sequence, starting at
|
jtulach@1348
|
850 |
* the append position, and appends them to the given string buffer. It is
|
jtulach@1348
|
851 |
* intended to be invoked after one or more invocations of the {@link
|
jtulach@1348
|
852 |
* #appendReplacement appendReplacement} method in order to copy the
|
jtulach@1348
|
853 |
* remainder of the input sequence. </p>
|
jtulach@1348
|
854 |
*
|
jtulach@1348
|
855 |
* @param sb
|
jtulach@1348
|
856 |
* The target string buffer
|
jtulach@1348
|
857 |
*
|
jtulach@1348
|
858 |
* @return The target string buffer
|
jtulach@1348
|
859 |
*/
|
jtulach@1348
|
860 |
public StringBuffer appendTail(StringBuffer sb) {
|
jtulach@1348
|
861 |
sb.append(text, lastAppendPosition, getTextLength());
|
jtulach@1348
|
862 |
return sb;
|
jtulach@1348
|
863 |
}
|
jtulach@1348
|
864 |
|
jtulach@1348
|
865 |
/**
|
jtulach@1348
|
866 |
* Replaces every subsequence of the input sequence that matches the
|
jtulach@1348
|
867 |
* pattern with the given replacement string.
|
jtulach@1348
|
868 |
*
|
jtulach@1348
|
869 |
* <p> This method first resets this matcher. It then scans the input
|
jtulach@1348
|
870 |
* sequence looking for matches of the pattern. Characters that are not
|
jtulach@1348
|
871 |
* part of any match are appended directly to the result string; each match
|
jtulach@1348
|
872 |
* is replaced in the result by the replacement string. The replacement
|
jtulach@1348
|
873 |
* string may contain references to captured subsequences as in the {@link
|
jtulach@1348
|
874 |
* #appendReplacement appendReplacement} method.
|
jtulach@1348
|
875 |
*
|
jtulach@1348
|
876 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
|
jtulach@1348
|
877 |
* the replacement string may cause the results to be different than if it
|
jtulach@1348
|
878 |
* were being treated as a literal replacement string. Dollar signs may be
|
jtulach@1348
|
879 |
* treated as references to captured subsequences as described above, and
|
jtulach@1348
|
880 |
* backslashes are used to escape literal characters in the replacement
|
jtulach@1348
|
881 |
* string.
|
jtulach@1348
|
882 |
*
|
jtulach@1348
|
883 |
* <p> Given the regular expression <tt>a*b</tt>, the input
|
jtulach@1348
|
884 |
* <tt>"aabfooaabfooabfoob"</tt>, and the replacement string
|
jtulach@1348
|
885 |
* <tt>"-"</tt>, an invocation of this method on a matcher for that
|
jtulach@1348
|
886 |
* expression would yield the string <tt>"-foo-foo-foo-"</tt>.
|
jtulach@1348
|
887 |
*
|
jtulach@1348
|
888 |
* <p> Invoking this method changes this matcher's state. If the matcher
|
jtulach@1348
|
889 |
* is to be used in further matching operations then it should first be
|
jtulach@1348
|
890 |
* reset. </p>
|
jtulach@1348
|
891 |
*
|
jtulach@1348
|
892 |
* @param replacement
|
jtulach@1348
|
893 |
* The replacement string
|
jtulach@1348
|
894 |
*
|
jtulach@1348
|
895 |
* @return The string constructed by replacing each matching subsequence
|
jtulach@1348
|
896 |
* by the replacement string, substituting captured subsequences
|
jtulach@1348
|
897 |
* as needed
|
jtulach@1348
|
898 |
*/
|
jtulach@1348
|
899 |
public String replaceAll(String replacement) {
|
jtulach@1348
|
900 |
reset();
|
jtulach@1348
|
901 |
boolean result = find();
|
jtulach@1348
|
902 |
if (result) {
|
jtulach@1348
|
903 |
StringBuffer sb = new StringBuffer();
|
jtulach@1348
|
904 |
do {
|
jtulach@1348
|
905 |
appendReplacement(sb, replacement);
|
jtulach@1348
|
906 |
result = find();
|
jtulach@1348
|
907 |
} while (result);
|
jtulach@1348
|
908 |
appendTail(sb);
|
jtulach@1348
|
909 |
return sb.toString();
|
jtulach@1348
|
910 |
}
|
jtulach@1348
|
911 |
return text.toString();
|
jtulach@1348
|
912 |
}
|
jtulach@1348
|
913 |
|
jtulach@1348
|
914 |
/**
|
jtulach@1348
|
915 |
* Replaces the first subsequence of the input sequence that matches the
|
jtulach@1348
|
916 |
* pattern with the given replacement string.
|
jtulach@1348
|
917 |
*
|
jtulach@1348
|
918 |
* <p> This method first resets this matcher. It then scans the input
|
jtulach@1348
|
919 |
* sequence looking for a match of the pattern. Characters that are not
|
jtulach@1348
|
920 |
* part of the match are appended directly to the result string; the match
|
jtulach@1348
|
921 |
* is replaced in the result by the replacement string. The replacement
|
jtulach@1348
|
922 |
* string may contain references to captured subsequences as in the {@link
|
jtulach@1348
|
923 |
* #appendReplacement appendReplacement} method.
|
jtulach@1348
|
924 |
*
|
jtulach@1348
|
925 |
* <p>Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
|
jtulach@1348
|
926 |
* the replacement string may cause the results to be different than if it
|
jtulach@1348
|
927 |
* were being treated as a literal replacement string. Dollar signs may be
|
jtulach@1348
|
928 |
* treated as references to captured subsequences as described above, and
|
jtulach@1348
|
929 |
* backslashes are used to escape literal characters in the replacement
|
jtulach@1348
|
930 |
* string.
|
jtulach@1348
|
931 |
*
|
jtulach@1348
|
932 |
* <p> Given the regular expression <tt>dog</tt>, the input
|
jtulach@1348
|
933 |
* <tt>"zzzdogzzzdogzzz"</tt>, and the replacement string
|
jtulach@1348
|
934 |
* <tt>"cat"</tt>, an invocation of this method on a matcher for that
|
jtulach@1348
|
935 |
* expression would yield the string <tt>"zzzcatzzzdogzzz"</tt>. </p>
|
jtulach@1348
|
936 |
*
|
jtulach@1348
|
937 |
* <p> Invoking this method changes this matcher's state. If the matcher
|
jtulach@1348
|
938 |
* is to be used in further matching operations then it should first be
|
jtulach@1348
|
939 |
* reset. </p>
|
jtulach@1348
|
940 |
*
|
jtulach@1348
|
941 |
* @param replacement
|
jtulach@1348
|
942 |
* The replacement string
|
jtulach@1348
|
943 |
* @return The string constructed by replacing the first matching
|
jtulach@1348
|
944 |
* subsequence by the replacement string, substituting captured
|
jtulach@1348
|
945 |
* subsequences as needed
|
jtulach@1348
|
946 |
*/
|
jtulach@1348
|
947 |
public String replaceFirst(String replacement) {
|
jtulach@1348
|
948 |
if (replacement == null)
|
jtulach@1348
|
949 |
throw new NullPointerException("replacement");
|
jtulach@1348
|
950 |
reset();
|
jtulach@1348
|
951 |
if (!find())
|
jtulach@1348
|
952 |
return text.toString();
|
jtulach@1348
|
953 |
StringBuffer sb = new StringBuffer();
|
jtulach@1348
|
954 |
appendReplacement(sb, replacement);
|
jtulach@1348
|
955 |
appendTail(sb);
|
jtulach@1348
|
956 |
return sb.toString();
|
jtulach@1348
|
957 |
}
|
jtulach@1348
|
958 |
|
jtulach@1348
|
959 |
/**
|
jtulach@1348
|
960 |
* Sets the limits of this matcher's region. The region is the part of the
|
jtulach@1348
|
961 |
* input sequence that will be searched to find a match. Invoking this
|
jtulach@1348
|
962 |
* method resets the matcher, and then sets the region to start at the
|
jtulach@1348
|
963 |
* index specified by the <code>start</code> parameter and end at the
|
jtulach@1348
|
964 |
* index specified by the <code>end</code> parameter.
|
jtulach@1348
|
965 |
*
|
jtulach@1348
|
966 |
* <p>Depending on the transparency and anchoring being used (see
|
jtulach@1348
|
967 |
* {@link #useTransparentBounds useTransparentBounds} and
|
jtulach@1348
|
968 |
* {@link #useAnchoringBounds useAnchoringBounds}), certain constructs such
|
jtulach@1348
|
969 |
* as anchors may behave differently at or around the boundaries of the
|
jtulach@1348
|
970 |
* region.
|
jtulach@1348
|
971 |
*
|
jtulach@1348
|
972 |
* @param start
|
jtulach@1348
|
973 |
* The index to start searching at (inclusive)
|
jtulach@1348
|
974 |
* @param end
|
jtulach@1348
|
975 |
* The index to end searching at (exclusive)
|
jtulach@1348
|
976 |
* @throws IndexOutOfBoundsException
|
jtulach@1348
|
977 |
* If start or end is less than zero, if
|
jtulach@1348
|
978 |
* start is greater than the length of the input sequence, if
|
jtulach@1348
|
979 |
* end is greater than the length of the input sequence, or if
|
jtulach@1348
|
980 |
* start is greater than end.
|
jtulach@1348
|
981 |
* @return this matcher
|
jtulach@1348
|
982 |
* @since 1.5
|
jtulach@1348
|
983 |
*/
|
jtulach@1348
|
984 |
public Matcher region(int start, int end) {
|
jtulach@1348
|
985 |
if ((start < 0) || (start > getTextLength()))
|
jtulach@1348
|
986 |
throw new IndexOutOfBoundsException("start");
|
jtulach@1348
|
987 |
if ((end < 0) || (end > getTextLength()))
|
jtulach@1348
|
988 |
throw new IndexOutOfBoundsException("end");
|
jtulach@1348
|
989 |
if (start > end)
|
jtulach@1348
|
990 |
throw new IndexOutOfBoundsException("start > end");
|
jtulach@1348
|
991 |
reset();
|
jtulach@1348
|
992 |
from = start;
|
jtulach@1348
|
993 |
to = end;
|
jtulach@1348
|
994 |
return this;
|
jtulach@1348
|
995 |
}
|
jtulach@1348
|
996 |
|
jtulach@1348
|
997 |
/**
|
jtulach@1348
|
998 |
* Reports the start index of this matcher's region. The
|
jtulach@1348
|
999 |
* searches this matcher conducts are limited to finding matches
|
jtulach@1348
|
1000 |
* within {@link #regionStart regionStart} (inclusive) and
|
jtulach@1348
|
1001 |
* {@link #regionEnd regionEnd} (exclusive).
|
jtulach@1348
|
1002 |
*
|
jtulach@1348
|
1003 |
* @return The starting point of this matcher's region
|
jtulach@1348
|
1004 |
* @since 1.5
|
jtulach@1348
|
1005 |
*/
|
jtulach@1348
|
1006 |
public int regionStart() {
|
jtulach@1348
|
1007 |
return from;
|
jtulach@1348
|
1008 |
}
|
jtulach@1348
|
1009 |
|
jtulach@1348
|
1010 |
/**
|
jtulach@1348
|
1011 |
* Reports the end index (exclusive) of this matcher's region.
|
jtulach@1348
|
1012 |
* The searches this matcher conducts are limited to finding matches
|
jtulach@1348
|
1013 |
* within {@link #regionStart regionStart} (inclusive) and
|
jtulach@1348
|
1014 |
* {@link #regionEnd regionEnd} (exclusive).
|
jtulach@1348
|
1015 |
*
|
jtulach@1348
|
1016 |
* @return the ending point of this matcher's region
|
jtulach@1348
|
1017 |
* @since 1.5
|
jtulach@1348
|
1018 |
*/
|
jtulach@1348
|
1019 |
public int regionEnd() {
|
jtulach@1348
|
1020 |
return to;
|
jtulach@1348
|
1021 |
}
|
jtulach@1348
|
1022 |
|
jtulach@1348
|
1023 |
/**
|
jtulach@1348
|
1024 |
* Queries the transparency of region bounds for this matcher.
|
jtulach@1348
|
1025 |
*
|
jtulach@1348
|
1026 |
* <p> This method returns <tt>true</tt> if this matcher uses
|
jtulach@1348
|
1027 |
* <i>transparent</i> bounds, <tt>false</tt> if it uses <i>opaque</i>
|
jtulach@1348
|
1028 |
* bounds.
|
jtulach@1348
|
1029 |
*
|
jtulach@1348
|
1030 |
* <p> See {@link #useTransparentBounds useTransparentBounds} for a
|
jtulach@1348
|
1031 |
* description of transparent and opaque bounds.
|
jtulach@1348
|
1032 |
*
|
jtulach@1348
|
1033 |
* <p> By default, a matcher uses opaque region boundaries.
|
jtulach@1348
|
1034 |
*
|
jtulach@1348
|
1035 |
* @return <tt>true</tt> iff this matcher is using transparent bounds,
|
jtulach@1348
|
1036 |
* <tt>false</tt> otherwise.
|
jtulach@1348
|
1037 |
* @see java.util.regex.Matcher#useTransparentBounds(boolean)
|
jtulach@1348
|
1038 |
* @since 1.5
|
jtulach@1348
|
1039 |
*/
|
jtulach@1348
|
1040 |
public boolean hasTransparentBounds() {
|
jtulach@1348
|
1041 |
return transparentBounds;
|
jtulach@1348
|
1042 |
}
|
jtulach@1348
|
1043 |
|
jtulach@1348
|
1044 |
/**
|
jtulach@1348
|
1045 |
* Sets the transparency of region bounds for this matcher.
|
jtulach@1348
|
1046 |
*
|
jtulach@1348
|
1047 |
* <p> Invoking this method with an argument of <tt>true</tt> will set this
|
jtulach@1348
|
1048 |
* matcher to use <i>transparent</i> bounds. If the boolean
|
jtulach@1348
|
1049 |
* argument is <tt>false</tt>, then <i>opaque</i> bounds will be used.
|
jtulach@1348
|
1050 |
*
|
jtulach@1348
|
1051 |
* <p> Using transparent bounds, the boundaries of this
|
jtulach@1348
|
1052 |
* matcher's region are transparent to lookahead, lookbehind,
|
jtulach@1348
|
1053 |
* and boundary matching constructs. Those constructs can see beyond the
|
jtulach@1348
|
1054 |
* boundaries of the region to see if a match is appropriate.
|
jtulach@1348
|
1055 |
*
|
jtulach@1348
|
1056 |
* <p> Using opaque bounds, the boundaries of this matcher's
|
jtulach@1348
|
1057 |
* region are opaque to lookahead, lookbehind, and boundary matching
|
jtulach@1348
|
1058 |
* constructs that may try to see beyond them. Those constructs cannot
|
jtulach@1348
|
1059 |
* look past the boundaries so they will fail to match anything outside
|
jtulach@1348
|
1060 |
* of the region.
|
jtulach@1348
|
1061 |
*
|
jtulach@1348
|
1062 |
* <p> By default, a matcher uses opaque bounds.
|
jtulach@1348
|
1063 |
*
|
jtulach@1348
|
1064 |
* @param b a boolean indicating whether to use opaque or transparent
|
jtulach@1348
|
1065 |
* regions
|
jtulach@1348
|
1066 |
* @return this matcher
|
jtulach@1348
|
1067 |
* @see java.util.regex.Matcher#hasTransparentBounds
|
jtulach@1348
|
1068 |
* @since 1.5
|
jtulach@1348
|
1069 |
*/
|
jtulach@1348
|
1070 |
public Matcher useTransparentBounds(boolean b) {
|
jtulach@1348
|
1071 |
transparentBounds = b;
|
jtulach@1348
|
1072 |
return this;
|
jtulach@1348
|
1073 |
}
|
jtulach@1348
|
1074 |
|
jtulach@1348
|
1075 |
/**
|
jtulach@1348
|
1076 |
* Queries the anchoring of region bounds for this matcher.
|
jtulach@1348
|
1077 |
*
|
jtulach@1348
|
1078 |
* <p> This method returns <tt>true</tt> if this matcher uses
|
jtulach@1348
|
1079 |
* <i>anchoring</i> bounds, <tt>false</tt> otherwise.
|
jtulach@1348
|
1080 |
*
|
jtulach@1348
|
1081 |
* <p> See {@link #useAnchoringBounds useAnchoringBounds} for a
|
jtulach@1348
|
1082 |
* description of anchoring bounds.
|
jtulach@1348
|
1083 |
*
|
jtulach@1348
|
1084 |
* <p> By default, a matcher uses anchoring region boundaries.
|
jtulach@1348
|
1085 |
*
|
jtulach@1348
|
1086 |
* @return <tt>true</tt> iff this matcher is using anchoring bounds,
|
jtulach@1348
|
1087 |
* <tt>false</tt> otherwise.
|
jtulach@1348
|
1088 |
* @see java.util.regex.Matcher#useAnchoringBounds(boolean)
|
jtulach@1348
|
1089 |
* @since 1.5
|
jtulach@1348
|
1090 |
*/
|
jtulach@1348
|
1091 |
public boolean hasAnchoringBounds() {
|
jtulach@1348
|
1092 |
return anchoringBounds;
|
jtulach@1348
|
1093 |
}
|
jtulach@1348
|
1094 |
|
jtulach@1348
|
1095 |
/**
|
jtulach@1348
|
1096 |
* Sets the anchoring of region bounds for this matcher.
|
jtulach@1348
|
1097 |
*
|
jtulach@1348
|
1098 |
* <p> Invoking this method with an argument of <tt>true</tt> will set this
|
jtulach@1348
|
1099 |
* matcher to use <i>anchoring</i> bounds. If the boolean
|
jtulach@1348
|
1100 |
* argument is <tt>false</tt>, then <i>non-anchoring</i> bounds will be
|
jtulach@1348
|
1101 |
* used.
|
jtulach@1348
|
1102 |
*
|
jtulach@1348
|
1103 |
* <p> Using anchoring bounds, the boundaries of this
|
jtulach@1348
|
1104 |
* matcher's region match anchors such as ^ and $.
|
jtulach@1348
|
1105 |
*
|
jtulach@1348
|
1106 |
* <p> Without anchoring bounds, the boundaries of this
|
jtulach@1348
|
1107 |
* matcher's region will not match anchors such as ^ and $.
|
jtulach@1348
|
1108 |
*
|
jtulach@1348
|
1109 |
* <p> By default, a matcher uses anchoring region boundaries.
|
jtulach@1348
|
1110 |
*
|
jtulach@1348
|
1111 |
* @param b a boolean indicating whether or not to use anchoring bounds.
|
jtulach@1348
|
1112 |
* @return this matcher
|
jtulach@1348
|
1113 |
* @see java.util.regex.Matcher#hasAnchoringBounds
|
jtulach@1348
|
1114 |
* @since 1.5
|
jtulach@1348
|
1115 |
*/
|
jtulach@1348
|
1116 |
public Matcher useAnchoringBounds(boolean b) {
|
jtulach@1348
|
1117 |
anchoringBounds = b;
|
jtulach@1348
|
1118 |
return this;
|
jtulach@1348
|
1119 |
}
|
jtulach@1348
|
1120 |
|
jtulach@1348
|
1121 |
/**
|
jtulach@1348
|
1122 |
* <p>Returns the string representation of this matcher. The
|
jtulach@1348
|
1123 |
* string representation of a <code>Matcher</code> contains information
|
jtulach@1348
|
1124 |
* that may be useful for debugging. The exact format is unspecified.
|
jtulach@1348
|
1125 |
*
|
jtulach@1348
|
1126 |
* @return The string representation of this matcher
|
jtulach@1348
|
1127 |
* @since 1.5
|
jtulach@1348
|
1128 |
*/
|
jtulach@1348
|
1129 |
public String toString() {
|
jtulach@1348
|
1130 |
StringBuilder sb = new StringBuilder();
|
jtulach@1348
|
1131 |
sb.append("java.util.regex.Matcher");
|
jtulach@1348
|
1132 |
sb.append("[pattern=" + pattern());
|
jtulach@1348
|
1133 |
sb.append(" region=");
|
jtulach@1348
|
1134 |
sb.append(regionStart() + "," + regionEnd());
|
jtulach@1348
|
1135 |
sb.append(" lastmatch=");
|
jtulach@1348
|
1136 |
if ((first >= 0) && (group() != null)) {
|
jtulach@1348
|
1137 |
sb.append(group());
|
jtulach@1348
|
1138 |
}
|
jtulach@1348
|
1139 |
sb.append("]");
|
jtulach@1348
|
1140 |
return sb.toString();
|
jtulach@1348
|
1141 |
}
|
jtulach@1348
|
1142 |
|
jtulach@1348
|
1143 |
/**
|
jtulach@1348
|
1144 |
* <p>Returns true if the end of input was hit by the search engine in
|
jtulach@1348
|
1145 |
* the last match operation performed by this matcher.
|
jtulach@1348
|
1146 |
*
|
jtulach@1348
|
1147 |
* <p>When this method returns true, then it is possible that more input
|
jtulach@1348
|
1148 |
* would have changed the result of the last search.
|
jtulach@1348
|
1149 |
*
|
jtulach@1348
|
1150 |
* @return true iff the end of input was hit in the last match; false
|
jtulach@1348
|
1151 |
* otherwise
|
jtulach@1348
|
1152 |
* @since 1.5
|
jtulach@1348
|
1153 |
*/
|
jtulach@1348
|
1154 |
public boolean hitEnd() {
|
jtulach@1348
|
1155 |
return hitEnd;
|
jtulach@1348
|
1156 |
}
|
jtulach@1348
|
1157 |
|
jtulach@1348
|
1158 |
/**
|
jtulach@1348
|
1159 |
* <p>Returns true if more input could change a positive match into a
|
jtulach@1348
|
1160 |
* negative one.
|
jtulach@1348
|
1161 |
*
|
jtulach@1348
|
1162 |
* <p>If this method returns true, and a match was found, then more
|
jtulach@1348
|
1163 |
* input could cause the match to be lost. If this method returns false
|
jtulach@1348
|
1164 |
* and a match was found, then more input might change the match but the
|
jtulach@1348
|
1165 |
* match won't be lost. If a match was not found, then requireEnd has no
|
jtulach@1348
|
1166 |
* meaning.
|
jtulach@1348
|
1167 |
*
|
jtulach@1348
|
1168 |
* @return true iff more input could change a positive match into a
|
jtulach@1348
|
1169 |
* negative one.
|
jtulach@1348
|
1170 |
* @since 1.5
|
jtulach@1348
|
1171 |
*/
|
jtulach@1348
|
1172 |
public boolean requireEnd() {
|
jtulach@1348
|
1173 |
return requireEnd;
|
jtulach@1348
|
1174 |
}
|
jtulach@1348
|
1175 |
|
jtulach@1348
|
1176 |
/**
|
jtulach@1348
|
1177 |
* Initiates a search to find a Pattern within the given bounds.
|
jtulach@1348
|
1178 |
* The groups are filled with default values and the match of the root
|
jtulach@1348
|
1179 |
* of the state machine is called. The state machine will hold the state
|
jtulach@1348
|
1180 |
* of the match as it proceeds in this matcher.
|
jtulach@1348
|
1181 |
*
|
jtulach@1348
|
1182 |
* Matcher.from is not set here, because it is the "hard" boundary
|
jtulach@1348
|
1183 |
* of the start of the search which anchors will set to. The from param
|
jtulach@1348
|
1184 |
* is the "soft" boundary of the start of the search, meaning that the
|
jtulach@1348
|
1185 |
* regex tries to match at that index but ^ won't match there. Subsequent
|
jtulach@1348
|
1186 |
* calls to the search methods start at a new "soft" boundary which is
|
jtulach@1348
|
1187 |
* the end of the previous match.
|
jtulach@1348
|
1188 |
*/
|
jtulach@1348
|
1189 |
boolean search(int from) {
|
jtulach@1348
|
1190 |
this.hitEnd = false;
|
jtulach@1348
|
1191 |
this.requireEnd = false;
|
jtulach@1348
|
1192 |
from = from < 0 ? 0 : from;
|
jtulach@1348
|
1193 |
this.first = from;
|
jtulach@1348
|
1194 |
this.oldLast = oldLast < 0 ? from : oldLast;
|
jtulach@1348
|
1195 |
for (int i = 0; i < groups.length; i++)
|
jtulach@1348
|
1196 |
groups[i] = -1;
|
jtulach@1348
|
1197 |
acceptMode = NOANCHOR;
|
jtulach@1348
|
1198 |
boolean result = parentPattern.root.match(this, from, text);
|
jtulach@1348
|
1199 |
if (!result)
|
jtulach@1348
|
1200 |
this.first = -1;
|
jtulach@1348
|
1201 |
this.oldLast = this.last;
|
jtulach@1348
|
1202 |
return result;
|
jtulach@1348
|
1203 |
}
|
jtulach@1348
|
1204 |
|
jtulach@1348
|
1205 |
/**
|
jtulach@1348
|
1206 |
* Initiates a search for an anchored match to a Pattern within the given
|
jtulach@1348
|
1207 |
* bounds. The groups are filled with default values and the match of the
|
jtulach@1348
|
1208 |
* root of the state machine is called. The state machine will hold the
|
jtulach@1348
|
1209 |
* state of the match as it proceeds in this matcher.
|
jtulach@1348
|
1210 |
*/
|
jtulach@1348
|
1211 |
boolean match(int from, int anchor) {
|
jtulach@1348
|
1212 |
this.hitEnd = false;
|
jtulach@1348
|
1213 |
this.requireEnd = false;
|
jtulach@1348
|
1214 |
from = from < 0 ? 0 : from;
|
jtulach@1348
|
1215 |
this.first = from;
|
jtulach@1348
|
1216 |
this.oldLast = oldLast < 0 ? from : oldLast;
|
jtulach@1348
|
1217 |
for (int i = 0; i < groups.length; i++)
|
jtulach@1348
|
1218 |
groups[i] = -1;
|
jtulach@1348
|
1219 |
acceptMode = anchor;
|
jtulach@1348
|
1220 |
boolean result = parentPattern.matchRoot.match(this, from, text);
|
jtulach@1348
|
1221 |
if (!result)
|
jtulach@1348
|
1222 |
this.first = -1;
|
jtulach@1348
|
1223 |
this.oldLast = this.last;
|
jtulach@1348
|
1224 |
return result;
|
jtulach@1348
|
1225 |
}
|
jtulach@1348
|
1226 |
|
jtulach@1348
|
1227 |
/**
|
jtulach@1348
|
1228 |
* Returns the end index of the text.
|
jtulach@1348
|
1229 |
*
|
jtulach@1348
|
1230 |
* @return the index after the last character in the text
|
jtulach@1348
|
1231 |
*/
|
jtulach@1348
|
1232 |
int getTextLength() {
|
jtulach@1348
|
1233 |
return text.length();
|
jtulach@1348
|
1234 |
}
|
jtulach@1348
|
1235 |
|
jtulach@1348
|
1236 |
/**
|
jtulach@1348
|
1237 |
* Generates a String from this Matcher's input in the specified range.
|
jtulach@1348
|
1238 |
*
|
jtulach@1348
|
1239 |
* @param beginIndex the beginning index, inclusive
|
jtulach@1348
|
1240 |
* @param endIndex the ending index, exclusive
|
jtulach@1348
|
1241 |
* @return A String generated from this Matcher's input
|
jtulach@1348
|
1242 |
*/
|
jtulach@1348
|
1243 |
CharSequence getSubSequence(int beginIndex, int endIndex) {
|
jtulach@1348
|
1244 |
return text.subSequence(beginIndex, endIndex);
|
jtulach@1348
|
1245 |
}
|
jtulach@1348
|
1246 |
|
jtulach@1348
|
1247 |
/**
|
jtulach@1348
|
1248 |
* Returns this Matcher's input character at index i.
|
jtulach@1348
|
1249 |
*
|
jtulach@1348
|
1250 |
* @return A char from the specified index
|
jtulach@1348
|
1251 |
*/
|
jtulach@1348
|
1252 |
char charAt(int i) {
|
jtulach@1348
|
1253 |
return text.charAt(i);
|
jtulach@1348
|
1254 |
}
|
jtulach@1348
|
1255 |
|
jtulach@1348
|
1256 |
}
|