author | psandoz |
Tue, 03 Mar 2015 12:30:48 +0100 | |
changeset 29243 | 80ea8d3d39d0 |
parent 25859 | 3317bb8137f4 |
child 29380 | c18777f9b6b9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.regex; |
|
27 |
||
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
28 |
import java.util.ConcurrentModificationException; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
29 |
import java.util.Iterator; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
30 |
import java.util.NoSuchElementException; |
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
31 |
import java.util.Objects; |
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
32 |
import java.util.Spliterator; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
33 |
import java.util.Spliterators; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
34 |
import java.util.function.Consumer; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
35 |
import java.util.function.Function; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
36 |
import java.util.stream.Stream; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
37 |
import java.util.stream.StreamSupport; |
2 | 38 |
|
39 |
/** |
|
18787 | 40 |
* An engine that performs match operations on a {@linkplain java.lang.CharSequence |
41 |
* character sequence} by interpreting a {@link Pattern}. |
|
2 | 42 |
* |
43 |
* <p> A matcher is created from a pattern by invoking the pattern's {@link |
|
44 |
* Pattern#matcher matcher} method. Once created, a matcher can be used to |
|
45 |
* perform three different kinds of match operations: |
|
46 |
* |
|
47 |
* <ul> |
|
48 |
* |
|
49 |
* <li><p> The {@link #matches matches} method attempts to match the entire |
|
50 |
* input sequence against the pattern. </p></li> |
|
51 |
* |
|
52 |
* <li><p> The {@link #lookingAt lookingAt} method attempts to match the |
|
53 |
* input sequence, starting at the beginning, against the pattern. </p></li> |
|
54 |
* |
|
55 |
* <li><p> The {@link #find find} method scans the input sequence looking for |
|
56 |
* the next subsequence that matches the pattern. </p></li> |
|
57 |
* |
|
58 |
* </ul> |
|
59 |
* |
|
60 |
* <p> Each of these methods returns a boolean indicating success or failure. |
|
61 |
* More information about a successful match can be obtained by querying the |
|
62 |
* state of the matcher. |
|
63 |
* |
|
64 |
* <p> A matcher finds matches in a subset of its input called the |
|
65 |
* <i>region</i>. By default, the region contains all of the matcher's input. |
|
66 |
* The region can be modified via the{@link #region region} method and queried |
|
67 |
* via the {@link #regionStart regionStart} and {@link #regionEnd regionEnd} |
|
68 |
* methods. The way that the region boundaries interact with some pattern |
|
69 |
* constructs can be changed. See {@link #useAnchoringBounds |
|
70 |
* useAnchoringBounds} and {@link #useTransparentBounds useTransparentBounds} |
|
71 |
* for more details. |
|
72 |
* |
|
73 |
* <p> This class also defines methods for replacing matched subsequences with |
|
74 |
* new strings whose contents can, if desired, be computed from the match |
|
75 |
* result. The {@link #appendReplacement appendReplacement} and {@link |
|
76 |
* #appendTail appendTail} methods can be used in tandem in order to collect |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
77 |
* the result into an existing string buffer or string builder. Alternatively, |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
78 |
* the more convenient {@link #replaceAll replaceAll} method can be used to |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
79 |
* create a string in which every matching subsequence in the input sequence |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
80 |
* is replaced. |
2 | 81 |
* |
82 |
* <p> The explicit state of a matcher includes the start and end indices of |
|
83 |
* the most recent successful match. It also includes the start and end |
|
84 |
* indices of the input subsequence captured by each <a |
|
85 |
* href="Pattern.html#cg">capturing group</a> in the pattern as well as a total |
|
86 |
* count of such subsequences. As a convenience, methods are also provided for |
|
87 |
* returning these captured subsequences in string form. |
|
88 |
* |
|
89 |
* <p> The explicit state of a matcher is initially undefined; attempting to |
|
90 |
* query any part of it before a successful match will cause an {@link |
|
91 |
* IllegalStateException} to be thrown. The explicit state of a matcher is |
|
92 |
* recomputed by every match operation. |
|
93 |
* |
|
94 |
* <p> The implicit state of a matcher includes the input character sequence as |
|
95 |
* well as the <i>append position</i>, which is initially zero and is updated |
|
96 |
* by the {@link #appendReplacement appendReplacement} method. |
|
97 |
* |
|
98 |
* <p> A matcher may be reset explicitly by invoking its {@link #reset()} |
|
99 |
* method or, if a new input sequence is desired, its {@link |
|
100 |
* #reset(java.lang.CharSequence) reset(CharSequence)} method. Resetting a |
|
101 |
* matcher discards its explicit state information and sets the append position |
|
102 |
* to zero. |
|
103 |
* |
|
104 |
* <p> Instances of this class are not safe for use by multiple concurrent |
|
105 |
* threads. </p> |
|
106 |
* |
|
107 |
* |
|
108 |
* @author Mike McCloskey |
|
109 |
* @author Mark Reinhold |
|
110 |
* @author JSR-51 Expert Group |
|
111 |
* @since 1.4 |
|
112 |
* @spec JSR-51 |
|
113 |
*/ |
|
114 |
||
115 |
public final class Matcher implements MatchResult { |
|
116 |
||
117 |
/** |
|
118 |
* The Pattern object that created this Matcher. |
|
119 |
*/ |
|
120 |
Pattern parentPattern; |
|
121 |
||
122 |
/** |
|
123 |
* The storage used by groups. They may contain invalid values if |
|
124 |
* a group was skipped during the matching. |
|
125 |
*/ |
|
126 |
int[] groups; |
|
127 |
||
128 |
/** |
|
129 |
* The range within the sequence that is to be matched. Anchors |
|
130 |
* will match at these "hard" boundaries. Changing the region |
|
131 |
* changes these values. |
|
132 |
*/ |
|
133 |
int from, to; |
|
134 |
||
135 |
/** |
|
136 |
* Lookbehind uses this value to ensure that the subexpression |
|
137 |
* match ends at the point where the lookbehind was encountered. |
|
138 |
*/ |
|
139 |
int lookbehindTo; |
|
140 |
||
141 |
/** |
|
142 |
* The original string being matched. |
|
143 |
*/ |
|
144 |
CharSequence text; |
|
145 |
||
146 |
/** |
|
147 |
* Matcher state used by the last node. NOANCHOR is used when a |
|
148 |
* match does not have to consume all of the input. ENDANCHOR is |
|
149 |
* the mode used for matching all the input. |
|
150 |
*/ |
|
151 |
static final int ENDANCHOR = 1; |
|
152 |
static final int NOANCHOR = 0; |
|
153 |
int acceptMode = NOANCHOR; |
|
154 |
||
155 |
/** |
|
156 |
* The range of string that last matched the pattern. If the last |
|
157 |
* match failed then first is -1; last initially holds 0 then it |
|
158 |
* holds the index of the end of the last match (which is where the |
|
159 |
* next search starts). |
|
160 |
*/ |
|
161 |
int first = -1, last = 0; |
|
162 |
||
163 |
/** |
|
164 |
* The end index of what matched in the last match operation. |
|
165 |
*/ |
|
166 |
int oldLast = -1; |
|
167 |
||
168 |
/** |
|
169 |
* The index of the last position appended in a substitution. |
|
170 |
*/ |
|
171 |
int lastAppendPosition = 0; |
|
172 |
||
173 |
/** |
|
174 |
* Storage used by nodes to tell what repetition they are on in |
|
175 |
* a pattern, and where groups begin. The nodes themselves are stateless, |
|
176 |
* so they rely on this field to hold state during a match. |
|
177 |
*/ |
|
178 |
int[] locals; |
|
179 |
||
180 |
/** |
|
181 |
* Boolean indicating whether or not more input could change |
|
182 |
* the results of the last match. |
|
183 |
* |
|
184 |
* If hitEnd is true, and a match was found, then more input |
|
185 |
* might cause a different match to be found. |
|
186 |
* If hitEnd is true and a match was not found, then more |
|
187 |
* input could cause a match to be found. |
|
188 |
* If hitEnd is false and a match was found, then more input |
|
189 |
* will not change the match. |
|
190 |
* If hitEnd is false and a match was not found, then more |
|
191 |
* input will not cause a match to be found. |
|
192 |
*/ |
|
193 |
boolean hitEnd; |
|
194 |
||
195 |
/** |
|
196 |
* Boolean indicating whether or not more input could change |
|
197 |
* a positive match into a negative one. |
|
198 |
* |
|
199 |
* If requireEnd is true, and a match was found, then more |
|
200 |
* input could cause the match to be lost. |
|
201 |
* If requireEnd is false and a match was found, then more |
|
202 |
* input might change the match but the match won't be lost. |
|
203 |
* If a match was not found, then requireEnd has no meaning. |
|
204 |
*/ |
|
205 |
boolean requireEnd; |
|
206 |
||
207 |
/** |
|
208 |
* If transparentBounds is true then the boundaries of this |
|
209 |
* matcher's region are transparent to lookahead, lookbehind, |
|
210 |
* and boundary matching constructs that try to see beyond them. |
|
211 |
*/ |
|
212 |
boolean transparentBounds = false; |
|
213 |
||
214 |
/** |
|
215 |
* If anchoringBounds is true then the boundaries of this |
|
216 |
* matcher's region match anchors such as ^ and $. |
|
217 |
*/ |
|
218 |
boolean anchoringBounds = true; |
|
219 |
||
220 |
/** |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
221 |
* Number of times this matcher's state has been modified |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
222 |
*/ |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
223 |
int modCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
224 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
225 |
/** |
2 | 226 |
* No default constructor. |
227 |
*/ |
|
228 |
Matcher() { |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* All matchers have the state used by Pattern during a match. |
|
233 |
*/ |
|
234 |
Matcher(Pattern parent, CharSequence text) { |
|
235 |
this.parentPattern = parent; |
|
236 |
this.text = text; |
|
237 |
||
238 |
// Allocate state storage |
|
239 |
int parentGroupCount = Math.max(parent.capturingGroupCount, 10); |
|
240 |
groups = new int[parentGroupCount * 2]; |
|
241 |
locals = new int[parent.localCount]; |
|
242 |
||
243 |
// Put fields into initial states |
|
244 |
reset(); |
|
245 |
} |
|
246 |
||
247 |
/** |
|
248 |
* Returns the pattern that is interpreted by this matcher. |
|
249 |
* |
|
250 |
* @return The pattern for which this matcher was created |
|
251 |
*/ |
|
252 |
public Pattern pattern() { |
|
253 |
return parentPattern; |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Returns the match state of this matcher as a {@link MatchResult}. |
|
258 |
* The result is unaffected by subsequent operations performed upon this |
|
259 |
* matcher. |
|
260 |
* |
|
261 |
* @return a <code>MatchResult</code> with the state of this matcher |
|
262 |
* @since 1.5 |
|
263 |
*/ |
|
264 |
public MatchResult toMatchResult() { |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
265 |
return toMatchResult(text.toString()); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
266 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
267 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
268 |
private MatchResult toMatchResult(String text) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
269 |
return new ImmutableMatchResult(this.first, |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
270 |
this.last, |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
271 |
groupCount(), |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
272 |
this.groups.clone(), |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
273 |
text); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
274 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
275 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
276 |
private static class ImmutableMatchResult implements MatchResult { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
277 |
private final int first; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
278 |
private final int last; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
279 |
private final int[] groups; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
280 |
private final int groupCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
281 |
private final String text; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
282 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
283 |
ImmutableMatchResult(int first, int last, int groupCount, |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
284 |
int groups[], String text) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
285 |
{ |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
286 |
this.first = first; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
287 |
this.last = last; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
288 |
this.groupCount = groupCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
289 |
this.groups = groups; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
290 |
this.text = text; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
291 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
292 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
293 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
294 |
public int start() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
295 |
return first; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
296 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
297 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
298 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
299 |
public int start(int group) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
300 |
if (group < 0 || group > groupCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
301 |
throw new IndexOutOfBoundsException("No group " + group); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
302 |
return groups[group * 2]; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
303 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
304 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
305 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
306 |
public int end() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
307 |
return last; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
308 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
309 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
310 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
311 |
public int end(int group) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
312 |
if (group < 0 || group > groupCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
313 |
throw new IndexOutOfBoundsException("No group " + group); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
314 |
return groups[group * 2 + 1]; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
315 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
316 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
317 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
318 |
public int groupCount() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
319 |
return groupCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
320 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
321 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
322 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
323 |
public String group() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
324 |
return group(0); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
325 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
326 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
327 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
328 |
public String group(int group) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
329 |
if (group < 0 || group > groupCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
330 |
throw new IndexOutOfBoundsException("No group " + group); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
331 |
if ((groups[group*2] == -1) || (groups[group*2+1] == -1)) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
332 |
return null; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
333 |
return text.subSequence(groups[group * 2], groups[group * 2 + 1]).toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
334 |
} |
2 | 335 |
} |
336 |
||
337 |
/** |
|
338 |
* Changes the <tt>Pattern</tt> that this <tt>Matcher</tt> uses to |
|
339 |
* find matches with. |
|
340 |
* |
|
341 |
* <p> This method causes this matcher to lose information |
|
342 |
* about the groups of the last match that occurred. The |
|
343 |
* matcher's position in the input is maintained and its |
|
344 |
* last append position is unaffected.</p> |
|
345 |
* |
|
346 |
* @param newPattern |
|
347 |
* The new pattern used by this matcher |
|
348 |
* @return This matcher |
|
349 |
* @throws IllegalArgumentException |
|
350 |
* If newPattern is <tt>null</tt> |
|
351 |
* @since 1.5 |
|
352 |
*/ |
|
353 |
public Matcher usePattern(Pattern newPattern) { |
|
354 |
if (newPattern == null) |
|
355 |
throw new IllegalArgumentException("Pattern cannot be null"); |
|
356 |
parentPattern = newPattern; |
|
357 |
||
358 |
// Reallocate state storage |
|
359 |
int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10); |
|
360 |
groups = new int[parentGroupCount * 2]; |
|
361 |
locals = new int[newPattern.localCount]; |
|
362 |
for (int i = 0; i < groups.length; i++) |
|
363 |
groups[i] = -1; |
|
364 |
for (int i = 0; i < locals.length; i++) |
|
365 |
locals[i] = -1; |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
366 |
modCount++; |
2 | 367 |
return this; |
368 |
} |
|
369 |
||
370 |
/** |
|
371 |
* Resets this matcher. |
|
372 |
* |
|
373 |
* <p> Resetting a matcher discards all of its explicit state information |
|
374 |
* and sets its append position to zero. The matcher's region is set to the |
|
375 |
* default region, which is its entire character sequence. The anchoring |
|
376 |
* and transparency of this matcher's region boundaries are unaffected. |
|
377 |
* |
|
378 |
* @return This matcher |
|
379 |
*/ |
|
380 |
public Matcher reset() { |
|
381 |
first = -1; |
|
382 |
last = 0; |
|
383 |
oldLast = -1; |
|
384 |
for(int i=0; i<groups.length; i++) |
|
385 |
groups[i] = -1; |
|
386 |
for(int i=0; i<locals.length; i++) |
|
387 |
locals[i] = -1; |
|
388 |
lastAppendPosition = 0; |
|
389 |
from = 0; |
|
390 |
to = getTextLength(); |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
391 |
modCount++; |
2 | 392 |
return this; |
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Resets this matcher with a new input sequence. |
|
397 |
* |
|
398 |
* <p> Resetting a matcher discards all of its explicit state information |
|
399 |
* and sets its append position to zero. The matcher's region is set to |
|
400 |
* the default region, which is its entire character sequence. The |
|
401 |
* anchoring and transparency of this matcher's region boundaries are |
|
402 |
* unaffected. |
|
403 |
* |
|
404 |
* @param input |
|
405 |
* The new input character sequence |
|
406 |
* |
|
407 |
* @return This matcher |
|
408 |
*/ |
|
409 |
public Matcher reset(CharSequence input) { |
|
410 |
text = input; |
|
411 |
return reset(); |
|
412 |
} |
|
413 |
||
414 |
/** |
|
18787 | 415 |
* Returns the start index of the previous match. |
2 | 416 |
* |
417 |
* @return The index of the first character matched |
|
418 |
* |
|
419 |
* @throws IllegalStateException |
|
420 |
* If no match has yet been attempted, |
|
421 |
* or if the previous match operation failed |
|
422 |
*/ |
|
423 |
public int start() { |
|
424 |
if (first < 0) |
|
425 |
throw new IllegalStateException("No match available"); |
|
426 |
return first; |
|
427 |
} |
|
428 |
||
429 |
/** |
|
430 |
* Returns the start index of the subsequence captured by the given group |
|
431 |
* during the previous match operation. |
|
432 |
* |
|
433 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left |
|
434 |
* to right, starting at one. Group zero denotes the entire pattern, so |
|
435 |
* the expression <i>m.</i><tt>start(0)</tt> is equivalent to |
|
436 |
* <i>m.</i><tt>start()</tt>. </p> |
|
437 |
* |
|
438 |
* @param group |
|
439 |
* The index of a capturing group in this matcher's pattern |
|
440 |
* |
|
441 |
* @return The index of the first character captured by the group, |
|
442 |
* or <tt>-1</tt> if the match was successful but the group |
|
443 |
* itself did not match anything |
|
444 |
* |
|
445 |
* @throws IllegalStateException |
|
446 |
* If no match has yet been attempted, |
|
447 |
* or if the previous match operation failed |
|
448 |
* |
|
449 |
* @throws IndexOutOfBoundsException |
|
450 |
* If there is no capturing group in the pattern |
|
451 |
* with the given index |
|
452 |
*/ |
|
453 |
public int start(int group) { |
|
454 |
if (first < 0) |
|
455 |
throw new IllegalStateException("No match available"); |
|
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
456 |
if (group < 0 || group > groupCount()) |
2 | 457 |
throw new IndexOutOfBoundsException("No group " + group); |
458 |
return groups[group * 2]; |
|
459 |
} |
|
460 |
||
461 |
/** |
|
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
462 |
* Returns the start index of the subsequence captured by the given |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
463 |
* <a href="Pattern.html#groupname">named-capturing group</a> during the |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
464 |
* previous match operation. |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
465 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
466 |
* @param name |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
467 |
* The name of a named-capturing group in this matcher's pattern |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
468 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
469 |
* @return The index of the first character captured by the group, |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
470 |
* or {@code -1} if the match was successful but the group |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
471 |
* itself did not match anything |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
472 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
473 |
* @throws IllegalStateException |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
474 |
* If no match has yet been attempted, |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
475 |
* or if the previous match operation failed |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
476 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
477 |
* @throws IllegalArgumentException |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
478 |
* If there is no capturing group in the pattern |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
479 |
* with the given name |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
480 |
* @since 1.8 |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
481 |
*/ |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
482 |
public int start(String name) { |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
483 |
return groups[getMatchedGroupIndex(name) * 2]; |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
484 |
} |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
485 |
|
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
486 |
/** |
18787 | 487 |
* Returns the offset after the last character matched. |
2 | 488 |
* |
489 |
* @return The offset after the last character matched |
|
490 |
* |
|
491 |
* @throws IllegalStateException |
|
492 |
* If no match has yet been attempted, |
|
493 |
* or if the previous match operation failed |
|
494 |
*/ |
|
495 |
public int end() { |
|
496 |
if (first < 0) |
|
497 |
throw new IllegalStateException("No match available"); |
|
498 |
return last; |
|
499 |
} |
|
500 |
||
501 |
/** |
|
502 |
* Returns the offset after the last character of the subsequence |
|
503 |
* captured by the given group during the previous match operation. |
|
504 |
* |
|
505 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left |
|
506 |
* to right, starting at one. Group zero denotes the entire pattern, so |
|
507 |
* the expression <i>m.</i><tt>end(0)</tt> is equivalent to |
|
508 |
* <i>m.</i><tt>end()</tt>. </p> |
|
509 |
* |
|
510 |
* @param group |
|
511 |
* The index of a capturing group in this matcher's pattern |
|
512 |
* |
|
513 |
* @return The offset after the last character captured by the group, |
|
514 |
* or <tt>-1</tt> if the match was successful |
|
515 |
* but the group itself did not match anything |
|
516 |
* |
|
517 |
* @throws IllegalStateException |
|
518 |
* If no match has yet been attempted, |
|
519 |
* or if the previous match operation failed |
|
520 |
* |
|
521 |
* @throws IndexOutOfBoundsException |
|
522 |
* If there is no capturing group in the pattern |
|
523 |
* with the given index |
|
524 |
*/ |
|
525 |
public int end(int group) { |
|
526 |
if (first < 0) |
|
527 |
throw new IllegalStateException("No match available"); |
|
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
528 |
if (group < 0 || group > groupCount()) |
2 | 529 |
throw new IndexOutOfBoundsException("No group " + group); |
530 |
return groups[group * 2 + 1]; |
|
531 |
} |
|
532 |
||
533 |
/** |
|
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
534 |
* Returns the offset after the last character of the subsequence |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
535 |
* captured by the given <a href="Pattern.html#groupname">named-capturing |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
536 |
* group</a> during the previous match operation. |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
537 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
538 |
* @param name |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
539 |
* The name of a named-capturing group in this matcher's pattern |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
540 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
541 |
* @return The offset after the last character captured by the group, |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
542 |
* or {@code -1} if the match was successful |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
543 |
* but the group itself did not match anything |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
544 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
545 |
* @throws IllegalStateException |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
546 |
* If no match has yet been attempted, |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
547 |
* or if the previous match operation failed |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
548 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
549 |
* @throws IllegalArgumentException |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
550 |
* If there is no capturing group in the pattern |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
551 |
* with the given name |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
552 |
* @since 1.8 |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
553 |
*/ |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
554 |
public int end(String name) { |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
555 |
return groups[getMatchedGroupIndex(name) * 2 + 1]; |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
556 |
} |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
557 |
|
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
558 |
/** |
2 | 559 |
* Returns the input subsequence matched by the previous match. |
560 |
* |
|
561 |
* <p> For a matcher <i>m</i> with input sequence <i>s</i>, |
|
562 |
* the expressions <i>m.</i><tt>group()</tt> and |
|
563 |
* <i>s.</i><tt>substring(</tt><i>m.</i><tt>start(),</tt> <i>m.</i><tt>end())</tt> |
|
564 |
* are equivalent. </p> |
|
565 |
* |
|
566 |
* <p> Note that some patterns, for example <tt>a*</tt>, match the empty |
|
567 |
* string. This method will return the empty string when the pattern |
|
568 |
* successfully matches the empty string in the input. </p> |
|
569 |
* |
|
570 |
* @return The (possibly empty) subsequence matched by the previous match, |
|
571 |
* in string form |
|
572 |
* |
|
573 |
* @throws IllegalStateException |
|
574 |
* If no match has yet been attempted, |
|
575 |
* or if the previous match operation failed |
|
576 |
*/ |
|
577 |
public String group() { |
|
578 |
return group(0); |
|
579 |
} |
|
580 |
||
581 |
/** |
|
582 |
* Returns the input subsequence captured by the given group during the |
|
583 |
* previous match operation. |
|
584 |
* |
|
585 |
* <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index |
|
586 |
* <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and |
|
587 |
* <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> |
|
588 |
* are equivalent. </p> |
|
589 |
* |
|
590 |
* <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left |
|
591 |
* to right, starting at one. Group zero denotes the entire pattern, so |
|
592 |
* the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>. |
|
593 |
* </p> |
|
594 |
* |
|
595 |
* <p> If the match was successful but the group specified failed to match |
|
596 |
* any part of the input sequence, then <tt>null</tt> is returned. Note |
|
597 |
* that some groups, for example <tt>(a*)</tt>, match the empty string. |
|
598 |
* This method will return the empty string when such a group successfully |
|
599 |
* matches the empty string in the input. </p> |
|
600 |
* |
|
601 |
* @param group |
|
602 |
* The index of a capturing group in this matcher's pattern |
|
603 |
* |
|
604 |
* @return The (possibly empty) subsequence captured by the group |
|
605 |
* during the previous match, or <tt>null</tt> if the group |
|
606 |
* failed to match part of the input |
|
607 |
* |
|
608 |
* @throws IllegalStateException |
|
609 |
* If no match has yet been attempted, |
|
610 |
* or if the previous match operation failed |
|
611 |
* |
|
612 |
* @throws IndexOutOfBoundsException |
|
613 |
* If there is no capturing group in the pattern |
|
614 |
* with the given index |
|
615 |
*/ |
|
616 |
public String group(int group) { |
|
617 |
if (first < 0) |
|
618 |
throw new IllegalStateException("No match found"); |
|
619 |
if (group < 0 || group > groupCount()) |
|
620 |
throw new IndexOutOfBoundsException("No group " + group); |
|
621 |
if ((groups[group*2] == -1) || (groups[group*2+1] == -1)) |
|
622 |
return null; |
|
623 |
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString(); |
|
624 |
} |
|
625 |
||
626 |
/** |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
627 |
* Returns the input subsequence captured by the given |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
628 |
* <a href="Pattern.html#groupname">named-capturing group</a> during the previous |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
629 |
* match operation. |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
630 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
631 |
* <p> If the match was successful but the group specified failed to match |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
632 |
* any part of the input sequence, then <tt>null</tt> is returned. Note |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
633 |
* that some groups, for example <tt>(a*)</tt>, match the empty string. |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
634 |
* This method will return the empty string when such a group successfully |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
635 |
* matches the empty string in the input. </p> |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
636 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
637 |
* @param name |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
638 |
* The name of a named-capturing group in this matcher's pattern |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
639 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
640 |
* @return The (possibly empty) subsequence captured by the named group |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
641 |
* during the previous match, or <tt>null</tt> if the group |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
642 |
* failed to match part of the input |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
643 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
644 |
* @throws IllegalStateException |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
645 |
* If no match has yet been attempted, |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
646 |
* or if the previous match operation failed |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
647 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
648 |
* @throws IllegalArgumentException |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
649 |
* If there is no capturing group in the pattern |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
650 |
* with the given name |
10345
b4aebbfc5b3a
7066490: @since 1.7 tag is missing for java.util.regex.Matcher.group(java.lang.String)
sherman
parents:
5506
diff
changeset
|
651 |
* @since 1.7 |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
652 |
*/ |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
653 |
public String group(String name) { |
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
654 |
int group = getMatchedGroupIndex(name); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
655 |
if ((groups[group*2] == -1) || (groups[group*2+1] == -1)) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
656 |
return null; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
657 |
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString(); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
658 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
659 |
|
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
660 |
/** |
2 | 661 |
* Returns the number of capturing groups in this matcher's pattern. |
662 |
* |
|
663 |
* <p> Group zero denotes the entire pattern by convention. It is not |
|
664 |
* included in this count. |
|
665 |
* |
|
666 |
* <p> Any non-negative integer smaller than or equal to the value |
|
667 |
* returned by this method is guaranteed to be a valid group index for |
|
668 |
* this matcher. </p> |
|
669 |
* |
|
670 |
* @return The number of capturing groups in this matcher's pattern |
|
671 |
*/ |
|
672 |
public int groupCount() { |
|
673 |
return parentPattern.capturingGroupCount - 1; |
|
674 |
} |
|
675 |
||
676 |
/** |
|
677 |
* Attempts to match the entire region against the pattern. |
|
678 |
* |
|
679 |
* <p> If the match succeeds then more information can be obtained via the |
|
680 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p> |
|
681 |
* |
|
682 |
* @return <tt>true</tt> if, and only if, the entire region sequence |
|
683 |
* matches this matcher's pattern |
|
684 |
*/ |
|
685 |
public boolean matches() { |
|
686 |
return match(from, ENDANCHOR); |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* Attempts to find the next subsequence of the input sequence that matches |
|
691 |
* the pattern. |
|
692 |
* |
|
693 |
* <p> This method starts at the beginning of this matcher's region, or, if |
|
694 |
* a previous invocation of the method was successful and the matcher has |
|
695 |
* not since been reset, at the first character not matched by the previous |
|
696 |
* match. |
|
697 |
* |
|
698 |
* <p> If the match succeeds then more information can be obtained via the |
|
699 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p> |
|
700 |
* |
|
701 |
* @return <tt>true</tt> if, and only if, a subsequence of the input |
|
702 |
* sequence matches this matcher's pattern |
|
703 |
*/ |
|
704 |
public boolean find() { |
|
705 |
int nextSearchIndex = last; |
|
706 |
if (nextSearchIndex == first) |
|
707 |
nextSearchIndex++; |
|
708 |
||
709 |
// If next search starts before region, start it at region |
|
710 |
if (nextSearchIndex < from) |
|
711 |
nextSearchIndex = from; |
|
712 |
||
713 |
// If next search starts beyond region then it fails |
|
714 |
if (nextSearchIndex > to) { |
|
715 |
for (int i = 0; i < groups.length; i++) |
|
716 |
groups[i] = -1; |
|
717 |
return false; |
|
718 |
} |
|
719 |
return search(nextSearchIndex); |
|
720 |
} |
|
721 |
||
722 |
/** |
|
723 |
* Resets this matcher and then attempts to find the next subsequence of |
|
724 |
* the input sequence that matches the pattern, starting at the specified |
|
725 |
* index. |
|
726 |
* |
|
727 |
* <p> If the match succeeds then more information can be obtained via the |
|
728 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent |
|
729 |
* invocations of the {@link #find()} method will start at the first |
|
730 |
* character not matched by this match. </p> |
|
731 |
* |
|
18787 | 732 |
* @param start the index to start searching for a match |
2 | 733 |
* @throws IndexOutOfBoundsException |
734 |
* If start is less than zero or if start is greater than the |
|
735 |
* length of the input sequence. |
|
736 |
* |
|
737 |
* @return <tt>true</tt> if, and only if, a subsequence of the input |
|
738 |
* sequence starting at the given index matches this matcher's |
|
739 |
* pattern |
|
740 |
*/ |
|
741 |
public boolean find(int start) { |
|
742 |
int limit = getTextLength(); |
|
743 |
if ((start < 0) || (start > limit)) |
|
744 |
throw new IndexOutOfBoundsException("Illegal start index"); |
|
745 |
reset(); |
|
746 |
return search(start); |
|
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Attempts to match the input sequence, starting at the beginning of the |
|
751 |
* region, against the pattern. |
|
752 |
* |
|
753 |
* <p> Like the {@link #matches matches} method, this method always starts |
|
754 |
* at the beginning of the region; unlike that method, it does not |
|
755 |
* require that the entire region be matched. |
|
756 |
* |
|
757 |
* <p> If the match succeeds then more information can be obtained via the |
|
758 |
* <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p> |
|
759 |
* |
|
760 |
* @return <tt>true</tt> if, and only if, a prefix of the input |
|
761 |
* sequence matches this matcher's pattern |
|
762 |
*/ |
|
763 |
public boolean lookingAt() { |
|
764 |
return match(from, NOANCHOR); |
|
765 |
} |
|
766 |
||
767 |
/** |
|
768 |
* Returns a literal replacement <code>String</code> for the specified |
|
769 |
* <code>String</code>. |
|
770 |
* |
|
771 |
* This method produces a <code>String</code> that will work |
|
772 |
* as a literal replacement <code>s</code> in the |
|
773 |
* <code>appendReplacement</code> method of the {@link Matcher} class. |
|
774 |
* The <code>String</code> produced will match the sequence of characters |
|
775 |
* in <code>s</code> treated as a literal sequence. Slashes ('\') and |
|
776 |
* dollar signs ('$') will be given no special meaning. |
|
777 |
* |
|
778 |
* @param s The string to be literalized |
|
779 |
* @return A literal string replacement |
|
780 |
* @since 1.5 |
|
781 |
*/ |
|
782 |
public static String quoteReplacement(String s) { |
|
783 |
if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1)) |
|
784 |
return s; |
|
785 |
StringBuilder sb = new StringBuilder(); |
|
786 |
for (int i=0; i<s.length(); i++) { |
|
787 |
char c = s.charAt(i); |
|
788 |
if (c == '\\' || c == '$') { |
|
789 |
sb.append('\\'); |
|
790 |
} |
|
791 |
sb.append(c); |
|
792 |
} |
|
793 |
return sb.toString(); |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Implements a non-terminal append-and-replace step. |
|
798 |
* |
|
799 |
* <p> This method performs the following actions: </p> |
|
800 |
* |
|
801 |
* <ol> |
|
802 |
* |
|
803 |
* <li><p> It reads characters from the input sequence, starting at the |
|
804 |
* append position, and appends them to the given string buffer. It |
|
805 |
* stops after reading the last character preceding the previous match, |
|
806 |
* that is, the character at index {@link |
|
807 |
* #start()} <tt>-</tt> <tt>1</tt>. </p></li> |
|
808 |
* |
|
809 |
* <li><p> It appends the given replacement string to the string buffer. |
|
810 |
* </p></li> |
|
811 |
* |
|
812 |
* <li><p> It sets the append position of this matcher to the index of |
|
813 |
* the last character matched, plus one, that is, to {@link #end()}. |
|
814 |
* </p></li> |
|
815 |
* |
|
816 |
* </ol> |
|
817 |
* |
|
818 |
* <p> The replacement string may contain references to subsequences |
|
819 |
* captured during the previous match: Each occurrence of |
|
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
820 |
* <tt>${</tt><i>name</i><tt>}</tt> or <tt>$</tt><i>g</i> |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
821 |
* will be replaced by the result of evaluating the corresponding |
18787 | 822 |
* {@link #group(String) group(name)} or {@link #group(int) group(g)} |
823 |
* respectively. For <tt>$</tt><i>g</i>, |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
824 |
* the first number after the <tt>$</tt> is always treated as part of |
2 | 825 |
* the group reference. Subsequent numbers are incorporated into g if |
826 |
* they would form a legal group reference. Only the numerals '0' |
|
827 |
* through '9' are considered as potential components of the group |
|
828 |
* reference. If the second group matched the string <tt>"foo"</tt>, for |
|
829 |
* example, then passing the replacement string <tt>"$2bar"</tt> would |
|
830 |
* cause <tt>"foobar"</tt> to be appended to the string buffer. A dollar |
|
831 |
* sign (<tt>$</tt>) may be included as a literal in the replacement |
|
832 |
* string by preceding it with a backslash (<tt>\$</tt>). |
|
833 |
* |
|
834 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
|
835 |
* the replacement string may cause the results to be different than if it |
|
836 |
* were being treated as a literal replacement string. Dollar signs may be |
|
837 |
* treated as references to captured subsequences as described above, and |
|
838 |
* backslashes are used to escape literal characters in the replacement |
|
839 |
* string. |
|
840 |
* |
|
841 |
* <p> This method is intended to be used in a loop together with the |
|
842 |
* {@link #appendTail appendTail} and {@link #find find} methods. The |
|
843 |
* following code, for example, writes <tt>one dog two dogs in the |
|
844 |
* yard</tt> to the standard-output stream: </p> |
|
845 |
* |
|
846 |
* <blockquote><pre> |
|
847 |
* Pattern p = Pattern.compile("cat"); |
|
848 |
* Matcher m = p.matcher("one cat two cats in the yard"); |
|
849 |
* StringBuffer sb = new StringBuffer(); |
|
850 |
* while (m.find()) { |
|
851 |
* m.appendReplacement(sb, "dog"); |
|
852 |
* } |
|
853 |
* m.appendTail(sb); |
|
854 |
* System.out.println(sb.toString());</pre></blockquote> |
|
855 |
* |
|
856 |
* @param sb |
|
857 |
* The target string buffer |
|
858 |
* |
|
859 |
* @param replacement |
|
860 |
* The replacement string |
|
861 |
* |
|
862 |
* @return This matcher |
|
863 |
* |
|
864 |
* @throws IllegalStateException |
|
865 |
* If no match has yet been attempted, |
|
866 |
* or if the previous match operation failed |
|
867 |
* |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
868 |
* @throws IllegalArgumentException |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
869 |
* If the replacement string refers to a named-capturing |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
870 |
* group that does not exist in the pattern |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
871 |
* |
2 | 872 |
* @throws IndexOutOfBoundsException |
873 |
* If the replacement string refers to a capturing group |
|
874 |
* that does not exist in the pattern |
|
875 |
*/ |
|
876 |
public Matcher appendReplacement(StringBuffer sb, String replacement) { |
|
877 |
// If no match, return error |
|
878 |
if (first < 0) |
|
879 |
throw new IllegalStateException("No match available"); |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
880 |
StringBuilder result = new StringBuilder(); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
881 |
appendExpandedReplacement(replacement, result); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
882 |
// Append the intervening text |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
883 |
sb.append(text, lastAppendPosition, first); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
884 |
// Append the match substitution |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
885 |
sb.append(result); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
886 |
lastAppendPosition = last; |
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
887 |
modCount++; |
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
888 |
return this; |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
889 |
} |
2 | 890 |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
891 |
/** |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
892 |
* Implements a non-terminal append-and-replace step. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
893 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
894 |
* <p> This method performs the following actions: </p> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
895 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
896 |
* <ol> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
897 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
898 |
* <li><p> It reads characters from the input sequence, starting at the |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
899 |
* append position, and appends them to the given string builder. It |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
900 |
* stops after reading the last character preceding the previous match, |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
901 |
* that is, the character at index {@link |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
902 |
* #start()} <tt>-</tt> <tt>1</tt>. </p></li> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
903 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
904 |
* <li><p> It appends the given replacement string to the string builder. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
905 |
* </p></li> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
906 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
907 |
* <li><p> It sets the append position of this matcher to the index of |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
908 |
* the last character matched, plus one, that is, to {@link #end()}. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
909 |
* </p></li> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
910 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
911 |
* </ol> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
912 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
913 |
* <p> The replacement string may contain references to subsequences |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
914 |
* captured during the previous match: Each occurrence of |
24196
61c9885d76e2
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql
yan
parents:
23734
diff
changeset
|
915 |
* <tt>$</tt><i>g</i> will be replaced by the result of |
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
916 |
* evaluating {@link #group(int) group}<tt>(</tt><i>g</i><tt>)</tt>. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
917 |
* The first number after the <tt>$</tt> is always treated as part of |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
918 |
* the group reference. Subsequent numbers are incorporated into g if |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
919 |
* they would form a legal group reference. Only the numerals '0' |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
920 |
* through '9' are considered as potential components of the group |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
921 |
* reference. If the second group matched the string <tt>"foo"</tt>, for |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
922 |
* example, then passing the replacement string <tt>"$2bar"</tt> would |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
923 |
* cause <tt>"foobar"</tt> to be appended to the string builder. A dollar |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
924 |
* sign (<tt>$</tt>) may be included as a literal in the replacement |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
925 |
* string by preceding it with a backslash (<tt>\$</tt>). |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
926 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
927 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
928 |
* the replacement string may cause the results to be different than if it |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
929 |
* were being treated as a literal replacement string. Dollar signs may be |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
930 |
* treated as references to captured subsequences as described above, and |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
931 |
* backslashes are used to escape literal characters in the replacement |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
932 |
* string. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
933 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
934 |
* <p> This method is intended to be used in a loop together with the |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
935 |
* {@link #appendTail appendTail} and {@link #find find} methods. The |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
936 |
* following code, for example, writes <tt>one dog two dogs in the |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
937 |
* yard</tt> to the standard-output stream: </p> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
938 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
939 |
* <blockquote><pre> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
940 |
* Pattern p = Pattern.compile("cat"); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
941 |
* Matcher m = p.matcher("one cat two cats in the yard"); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
942 |
* StringBuilder sb = new StringBuilder(); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
943 |
* while (m.find()) { |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
944 |
* m.appendReplacement(sb, "dog"); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
945 |
* } |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
946 |
* m.appendTail(sb); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
947 |
* System.out.println(sb.toString());</pre></blockquote> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
948 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
949 |
* @param sb |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
950 |
* The target string builder |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
951 |
* @param replacement |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
952 |
* The replacement string |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
953 |
* @return This matcher |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
954 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
955 |
* @throws IllegalStateException |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
956 |
* If no match has yet been attempted, |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
957 |
* or if the previous match operation failed |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
958 |
* @throws IllegalArgumentException |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
959 |
* If the replacement string refers to a named-capturing |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
960 |
* group that does not exist in the pattern |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
961 |
* @throws IndexOutOfBoundsException |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
962 |
* If the replacement string refers to a capturing group |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
963 |
* that does not exist in the pattern |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
964 |
* @since 1.9 |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
965 |
*/ |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
966 |
public Matcher appendReplacement(StringBuilder sb, String replacement) { |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
967 |
// If no match, return error |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
968 |
if (first < 0) |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
969 |
throw new IllegalStateException("No match available"); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
970 |
StringBuilder result = new StringBuilder(); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
971 |
appendExpandedReplacement(replacement, result); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
972 |
// Append the intervening text |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
973 |
sb.append(text, lastAppendPosition, first); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
974 |
// Append the match substitution |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
975 |
sb.append(result); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
976 |
lastAppendPosition = last; |
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
977 |
modCount++; |
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
978 |
return this; |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
979 |
} |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
980 |
|
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
981 |
/** |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
982 |
* Processes replacement string to replace group references with |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
983 |
* groups. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
984 |
*/ |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
985 |
private StringBuilder appendExpandedReplacement( |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
986 |
String replacement, StringBuilder result) { |
2 | 987 |
int cursor = 0; |
988 |
while (cursor < replacement.length()) { |
|
989 |
char nextChar = replacement.charAt(cursor); |
|
990 |
if (nextChar == '\\') { |
|
991 |
cursor++; |
|
12432
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
992 |
if (cursor == replacement.length()) |
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
993 |
throw new IllegalArgumentException( |
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
994 |
"character to be escaped is missing"); |
2 | 995 |
nextChar = replacement.charAt(cursor); |
996 |
result.append(nextChar); |
|
997 |
cursor++; |
|
998 |
} else if (nextChar == '$') { |
|
999 |
// Skip past $ |
|
1000 |
cursor++; |
|
12432
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
1001 |
// Throw IAE if this "$" is the last character in replacement |
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
1002 |
if (cursor == replacement.length()) |
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
1003 |
throw new IllegalArgumentException( |
748fcaa0e0c1
7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents:
10345
diff
changeset
|
1004 |
"Illegal group reference: group index is missing"); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1005 |
nextChar = replacement.charAt(cursor); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1006 |
int refNum = -1; |
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1007 |
if (nextChar == '{') { |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1008 |
cursor++; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1009 |
StringBuilder gsb = new StringBuilder(); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1010 |
while (cursor < replacement.length()) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1011 |
nextChar = replacement.charAt(cursor); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1012 |
if (ASCII.isLower(nextChar) || |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1013 |
ASCII.isUpper(nextChar) || |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1014 |
ASCII.isDigit(nextChar)) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1015 |
gsb.append(nextChar); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1016 |
cursor++; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1017 |
} else { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1018 |
break; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1019 |
} |
2 | 1020 |
} |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1021 |
if (gsb.length() == 0) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1022 |
throw new IllegalArgumentException( |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1023 |
"named capturing group has 0 length name"); |
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1024 |
if (nextChar != '}') |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1025 |
throw new IllegalArgumentException( |
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1026 |
"named capturing group is missing trailing '}'"); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1027 |
String gname = gsb.toString(); |
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1028 |
if (ASCII.isDigit(gname.charAt(0))) |
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1029 |
throw new IllegalArgumentException( |
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1030 |
"capturing group name {" + gname + |
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1031 |
"} starts with digit character"); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1032 |
if (!parentPattern.namedGroups().containsKey(gname)) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1033 |
throw new IllegalArgumentException( |
4161
679d00486dc6
6878475: Better syntax for the named capture group in regex
sherman
parents:
2163
diff
changeset
|
1034 |
"No group with name {" + gname + "}"); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1035 |
refNum = parentPattern.namedGroups().get(gname); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1036 |
cursor++; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1037 |
} else { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1038 |
// The first number is always a group |
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1039 |
refNum = nextChar - '0'; |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1040 |
if ((refNum < 0) || (refNum > 9)) |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1041 |
throw new IllegalArgumentException( |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1042 |
"Illegal group reference"); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1043 |
cursor++; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1044 |
// Capture the largest legal group string |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1045 |
boolean done = false; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1046 |
while (!done) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1047 |
if (cursor >= replacement.length()) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1048 |
break; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1049 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1050 |
int nextDigit = replacement.charAt(cursor) - '0'; |
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1051 |
if ((nextDigit < 0) || (nextDigit > 9)) { // not a number |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1052 |
break; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1053 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1054 |
int newRefNum = (refNum * 10) + nextDigit; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1055 |
if (groupCount() < newRefNum) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1056 |
done = true; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1057 |
} else { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1058 |
refNum = newRefNum; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1059 |
cursor++; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1060 |
} |
2 | 1061 |
} |
1062 |
} |
|
1063 |
// Append group |
|
1064 |
if (start(refNum) != -1 && end(refNum) != -1) |
|
1065 |
result.append(text, start(refNum), end(refNum)); |
|
1066 |
} else { |
|
1067 |
result.append(nextChar); |
|
1068 |
cursor++; |
|
1069 |
} |
|
1070 |
} |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1071 |
return result; |
2 | 1072 |
} |
1073 |
||
1074 |
/** |
|
1075 |
* Implements a terminal append-and-replace step. |
|
1076 |
* |
|
1077 |
* <p> This method reads characters from the input sequence, starting at |
|
1078 |
* the append position, and appends them to the given string buffer. It is |
|
1079 |
* intended to be invoked after one or more invocations of the {@link |
|
1080 |
* #appendReplacement appendReplacement} method in order to copy the |
|
1081 |
* remainder of the input sequence. </p> |
|
1082 |
* |
|
1083 |
* @param sb |
|
1084 |
* The target string buffer |
|
1085 |
* |
|
1086 |
* @return The target string buffer |
|
1087 |
*/ |
|
1088 |
public StringBuffer appendTail(StringBuffer sb) { |
|
1089 |
sb.append(text, lastAppendPosition, getTextLength()); |
|
1090 |
return sb; |
|
1091 |
} |
|
1092 |
||
1093 |
/** |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1094 |
* Implements a terminal append-and-replace step. |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1095 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1096 |
* <p> This method reads characters from the input sequence, starting at |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1097 |
* the append position, and appends them to the given string builder. It is |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1098 |
* intended to be invoked after one or more invocations of the {@link |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1099 |
* #appendReplacement appendReplacement} method in order to copy the |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1100 |
* remainder of the input sequence. </p> |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1101 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1102 |
* @param sb |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1103 |
* The target string builder |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1104 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1105 |
* @return The target string builder |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1106 |
* |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1107 |
* @since 1.9 |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1108 |
*/ |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1109 |
public StringBuilder appendTail(StringBuilder sb) { |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1110 |
sb.append(text, lastAppendPosition, getTextLength()); |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1111 |
return sb; |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1112 |
} |
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1113 |
|
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1114 |
/** |
2 | 1115 |
* Replaces every subsequence of the input sequence that matches the |
1116 |
* pattern with the given replacement string. |
|
1117 |
* |
|
1118 |
* <p> This method first resets this matcher. It then scans the input |
|
1119 |
* sequence looking for matches of the pattern. Characters that are not |
|
1120 |
* part of any match are appended directly to the result string; each match |
|
1121 |
* is replaced in the result by the replacement string. The replacement |
|
1122 |
* string may contain references to captured subsequences as in the {@link |
|
1123 |
* #appendReplacement appendReplacement} method. |
|
1124 |
* |
|
1125 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
|
1126 |
* the replacement string may cause the results to be different than if it |
|
1127 |
* were being treated as a literal replacement string. Dollar signs may be |
|
1128 |
* treated as references to captured subsequences as described above, and |
|
1129 |
* backslashes are used to escape literal characters in the replacement |
|
1130 |
* string. |
|
1131 |
* |
|
1132 |
* <p> Given the regular expression <tt>a*b</tt>, the input |
|
1133 |
* <tt>"aabfooaabfooabfoob"</tt>, and the replacement string |
|
1134 |
* <tt>"-"</tt>, an invocation of this method on a matcher for that |
|
1135 |
* expression would yield the string <tt>"-foo-foo-foo-"</tt>. |
|
1136 |
* |
|
1137 |
* <p> Invoking this method changes this matcher's state. If the matcher |
|
1138 |
* is to be used in further matching operations then it should first be |
|
1139 |
* reset. </p> |
|
1140 |
* |
|
1141 |
* @param replacement |
|
1142 |
* The replacement string |
|
1143 |
* |
|
1144 |
* @return The string constructed by replacing each matching subsequence |
|
1145 |
* by the replacement string, substituting captured subsequences |
|
1146 |
* as needed |
|
1147 |
*/ |
|
1148 |
public String replaceAll(String replacement) { |
|
1149 |
reset(); |
|
1150 |
boolean result = find(); |
|
1151 |
if (result) { |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1152 |
StringBuilder sb = new StringBuilder(); |
2 | 1153 |
do { |
1154 |
appendReplacement(sb, replacement); |
|
1155 |
result = find(); |
|
1156 |
} while (result); |
|
1157 |
appendTail(sb); |
|
1158 |
return sb.toString(); |
|
1159 |
} |
|
1160 |
return text.toString(); |
|
1161 |
} |
|
1162 |
||
1163 |
/** |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1164 |
* Replaces every subsequence of the input sequence that matches the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1165 |
* pattern with the result of applying the given replacer function to the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1166 |
* match result of this matcher corresponding to that subsequence. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1167 |
* Exceptions thrown by the function are relayed to the caller. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1168 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1169 |
* <p> This method first resets this matcher. It then scans the input |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1170 |
* sequence looking for matches of the pattern. Characters that are not |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1171 |
* part of any match are appended directly to the result string; each match |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1172 |
* is replaced in the result by the applying the replacer function that |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1173 |
* returns a replacement string. Each replacement string may contain |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1174 |
* references to captured subsequences as in the {@link #appendReplacement |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1175 |
* appendReplacement} method. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1176 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1177 |
* <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1178 |
* a replacement string may cause the results to be different than if it |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1179 |
* were being treated as a literal replacement string. Dollar signs may be |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1180 |
* treated as references to captured subsequences as described above, and |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1181 |
* backslashes are used to escape literal characters in the replacement |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1182 |
* string. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1183 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1184 |
* <p> Given the regular expression <tt>dog</tt>, the input |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1185 |
* <tt>"zzzdogzzzdogzzz"</tt>, and the function |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1186 |
* <tt>mr -> mr.group().toUpperCase()</tt>, an invocation of this method on |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1187 |
* a matcher for that expression would yield the string |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1188 |
* <tt>"zzzDOGzzzDOGzzz"</tt>. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1189 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1190 |
* <p> Invoking this method changes this matcher's state. If the matcher |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1191 |
* is to be used in further matching operations then it should first be |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1192 |
* reset. </p> |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1193 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1194 |
* <p> The replacer function should not modify this matcher's state during |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1195 |
* replacement. This method will, on a best-effort basis, throw a |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1196 |
* {@link java.util.ConcurrentModificationException} if such modification is |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1197 |
* detected. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1198 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1199 |
* <p> The state of each match result passed to the replacer function is |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1200 |
* guaranteed to be constant only for the duration of the replacer function |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1201 |
* call and only if the replacer function does not modify this matcher's |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1202 |
* state. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1203 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1204 |
* @implNote |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1205 |
* This implementation applies the replacer function to this matcher, which |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1206 |
* is an instance of {@code MatchResult}. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1207 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1208 |
* @param replacer |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1209 |
* The function to be applied to the match result of this matcher |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1210 |
* that returns a replacement string. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1211 |
* @return The string constructed by replacing each matching subsequence |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1212 |
* with the result of applying the replacer function to that |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1213 |
* matched subsequence, substituting captured subsequences as |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1214 |
* needed. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1215 |
* @throws NullPointerException if the replacer function is null |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1216 |
* @throws ConcurrentModificationException if it is detected, on a |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1217 |
* best-effort basis, that the replacer function modified this |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1218 |
* matcher's state |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1219 |
* @since 1.9 |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1220 |
*/ |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1221 |
public String replaceAll(Function<MatchResult, String> replacer) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1222 |
Objects.requireNonNull(replacer); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1223 |
reset(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1224 |
boolean result = find(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1225 |
if (result) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1226 |
StringBuilder sb = new StringBuilder(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1227 |
do { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1228 |
int ec = modCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1229 |
String replacement = replacer.apply(this); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1230 |
if (ec != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1231 |
throw new ConcurrentModificationException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1232 |
appendReplacement(sb, replacement); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1233 |
result = find(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1234 |
} while (result); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1235 |
appendTail(sb); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1236 |
return sb.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1237 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1238 |
return text.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1239 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1240 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1241 |
/** |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1242 |
* Returns a stream of match results for each subsequence of the input |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1243 |
* sequence that matches the pattern. The match results occur in the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1244 |
* same order as the matching subsequences in the input sequence. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1245 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1246 |
* <p> Each match result is produced as if by {@link #toMatchResult()}. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1247 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1248 |
* <p> This method does not reset this matcher. Matching starts on |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1249 |
* initiation of the terminal stream operation either at the beginning of |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1250 |
* this matcher's region, or, if the matcher has not since been reset, at |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1251 |
* the first character not matched by a previous match. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1252 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1253 |
* <p> If the matcher is to be used for further matching operations after |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1254 |
* the terminal stream operation completes then it should be first reset. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1255 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1256 |
* <p> This matcher's state should not be modified during execution of the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1257 |
* returned stream's pipeline. The returned stream's source |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1258 |
* {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1259 |
* basis, throw a {@link java.util.ConcurrentModificationException} if such |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1260 |
* modification is detected. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1261 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1262 |
* @return a sequential stream of match results. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1263 |
* @since 1.9 |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1264 |
*/ |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1265 |
public Stream<MatchResult> results() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1266 |
class MatchResultIterator implements Iterator<MatchResult> { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1267 |
// -ve for call to find, 0 for not found, 1 for found |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1268 |
int state = -1; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1269 |
// State for concurrent modification checking |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1270 |
// -1 for uninitialized |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1271 |
int expectedCount = -1; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1272 |
// The input sequence as a string, set once only after first find |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1273 |
// Avoids repeated conversion from CharSequence for each match |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1274 |
String textAsString; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1275 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1276 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1277 |
public MatchResult next() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1278 |
if (expectedCount >= 0 && expectedCount != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1279 |
throw new ConcurrentModificationException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1280 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1281 |
if (!hasNext()) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1282 |
throw new NoSuchElementException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1283 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1284 |
state = -1; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1285 |
return toMatchResult(textAsString); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1286 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1287 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1288 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1289 |
public boolean hasNext() { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1290 |
if (state >= 0) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1291 |
return state == 1; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1292 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1293 |
// Defer throwing ConcurrentModificationException to when next |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1294 |
// or forEachRemaining is called. The is consistent with other |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1295 |
// fail-fast implementations. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1296 |
if (expectedCount >= 0 && expectedCount != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1297 |
return true; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1298 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1299 |
boolean found = find(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1300 |
// Capture the input sequence as a string on first find |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1301 |
if (found && state < 0) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1302 |
textAsString = text.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1303 |
state = found ? 1 : 0; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1304 |
expectedCount = modCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1305 |
return found; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1306 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1307 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1308 |
@Override |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1309 |
public void forEachRemaining(Consumer<? super MatchResult> action) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1310 |
if (expectedCount >= 0 && expectedCount != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1311 |
throw new ConcurrentModificationException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1312 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1313 |
int s = state; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1314 |
if (s == 0) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1315 |
return; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1316 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1317 |
// Set state to report no more elements on further operations |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1318 |
state = 0; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1319 |
expectedCount = -1; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1320 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1321 |
// Perform a first find if required |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1322 |
if (s < 0 && !find()) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1323 |
return; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1324 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1325 |
// Capture the input sequence as a string on first find |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1326 |
textAsString = text.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1327 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1328 |
do { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1329 |
int ec = modCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1330 |
action.accept(toMatchResult(textAsString)); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1331 |
if (ec != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1332 |
throw new ConcurrentModificationException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1333 |
} while (find()); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1334 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1335 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1336 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1337 |
new MatchResultIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1338 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1339 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1340 |
/** |
2 | 1341 |
* Replaces the first subsequence of the input sequence that matches the |
1342 |
* pattern with the given replacement string. |
|
1343 |
* |
|
1344 |
* <p> This method first resets this matcher. It then scans the input |
|
1345 |
* sequence looking for a match of the pattern. Characters that are not |
|
1346 |
* part of the match are appended directly to the result string; the match |
|
1347 |
* is replaced in the result by the replacement string. The replacement |
|
1348 |
* string may contain references to captured subsequences as in the {@link |
|
1349 |
* #appendReplacement appendReplacement} method. |
|
1350 |
* |
|
1351 |
* <p>Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
|
1352 |
* the replacement string may cause the results to be different than if it |
|
1353 |
* were being treated as a literal replacement string. Dollar signs may be |
|
1354 |
* treated as references to captured subsequences as described above, and |
|
1355 |
* backslashes are used to escape literal characters in the replacement |
|
1356 |
* string. |
|
1357 |
* |
|
1358 |
* <p> Given the regular expression <tt>dog</tt>, the input |
|
1359 |
* <tt>"zzzdogzzzdogzzz"</tt>, and the replacement string |
|
1360 |
* <tt>"cat"</tt>, an invocation of this method on a matcher for that |
|
1361 |
* expression would yield the string <tt>"zzzcatzzzdogzzz"</tt>. </p> |
|
1362 |
* |
|
1363 |
* <p> Invoking this method changes this matcher's state. If the matcher |
|
1364 |
* is to be used in further matching operations then it should first be |
|
1365 |
* reset. </p> |
|
1366 |
* |
|
1367 |
* @param replacement |
|
1368 |
* The replacement string |
|
1369 |
* @return The string constructed by replacing the first matching |
|
1370 |
* subsequence by the replacement string, substituting captured |
|
1371 |
* subsequences as needed |
|
1372 |
*/ |
|
1373 |
public String replaceFirst(String replacement) { |
|
1374 |
if (replacement == null) |
|
1375 |
throw new NullPointerException("replacement"); |
|
1376 |
reset(); |
|
1377 |
if (!find()) |
|
1378 |
return text.toString(); |
|
23734
439905b27f94
8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents:
18787
diff
changeset
|
1379 |
StringBuilder sb = new StringBuilder(); |
2 | 1380 |
appendReplacement(sb, replacement); |
1381 |
appendTail(sb); |
|
1382 |
return sb.toString(); |
|
1383 |
} |
|
1384 |
||
1385 |
/** |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1386 |
* Replaces the first subsequence of the input sequence that matches the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1387 |
* pattern with the result of applying the given replacer function to the |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1388 |
* match result of this matcher corresponding to that subsequence. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1389 |
* Exceptions thrown by the replace function are relayed to the caller. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1390 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1391 |
* <p> This method first resets this matcher. It then scans the input |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1392 |
* sequence looking for a match of the pattern. Characters that are not |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1393 |
* part of the match are appended directly to the result string; the match |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1394 |
* is replaced in the result by the applying the replacer function that |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1395 |
* returns a replacement string. The replacement string may contain |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1396 |
* references to captured subsequences as in the {@link #appendReplacement |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1397 |
* appendReplacement} method. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1398 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1399 |
* <p>Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1400 |
* the replacement string may cause the results to be different than if it |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1401 |
* were being treated as a literal replacement string. Dollar signs may be |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1402 |
* treated as references to captured subsequences as described above, and |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1403 |
* backslashes are used to escape literal characters in the replacement |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1404 |
* string. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1405 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1406 |
* <p> Given the regular expression <tt>dog</tt>, the input |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1407 |
* <tt>"zzzdogzzzdogzzz"</tt>, and the function |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1408 |
* <tt>mr -> mr.group().toUpperCase()</tt>, an invocation of this method on |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1409 |
* a matcher for that expression would yield the string |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1410 |
* <tt>"zzzDOGzzzdogzzz"</tt>. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1411 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1412 |
* <p> Invoking this method changes this matcher's state. If the matcher |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1413 |
* is to be used in further matching operations then it should first be |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1414 |
* reset. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1415 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1416 |
* <p> The replacer function should not modify this matcher's state during |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1417 |
* replacement. This method will, on a best-effort basis, throw a |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1418 |
* {@link java.util.ConcurrentModificationException} if such modification is |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1419 |
* detected. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1420 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1421 |
* <p> The state of the match result passed to the replacer function is |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1422 |
* guaranteed to be constant only for the duration of the replacer function |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1423 |
* call and only if the replacer function does not modify this matcher's |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1424 |
* state. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1425 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1426 |
* @implNote |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1427 |
* This implementation applies the replacer function to this matcher, which |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1428 |
* is an instance of {@code MatchResult}. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1429 |
* |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1430 |
* @param replacer |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1431 |
* The function to be applied to the match result of this matcher |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1432 |
* that returns a replacement string. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1433 |
* @return The string constructed by replacing the first matching |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1434 |
* subsequence with the result of applying the replacer function to |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1435 |
* the matched subsequence, substituting captured subsequences as |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1436 |
* needed. |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1437 |
* @throws NullPointerException if the replacer function is null |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1438 |
* @throws ConcurrentModificationException if it is detected, on a |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1439 |
* best-effort basis, that the replacer function modified this |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1440 |
* matcher's state |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1441 |
* @since 1.9 |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1442 |
*/ |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1443 |
public String replaceFirst(Function<MatchResult, String> replacer) { |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1444 |
Objects.requireNonNull(replacer); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1445 |
reset(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1446 |
if (!find()) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1447 |
return text.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1448 |
StringBuilder sb = new StringBuilder(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1449 |
int ec = modCount; |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1450 |
String replacement = replacer.apply(this); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1451 |
if (ec != modCount) |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1452 |
throw new ConcurrentModificationException(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1453 |
appendReplacement(sb, replacement); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1454 |
appendTail(sb); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1455 |
return sb.toString(); |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1456 |
} |
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1457 |
|
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1458 |
/** |
2 | 1459 |
* Sets the limits of this matcher's region. The region is the part of the |
1460 |
* input sequence that will be searched to find a match. Invoking this |
|
1461 |
* method resets the matcher, and then sets the region to start at the |
|
1462 |
* index specified by the <code>start</code> parameter and end at the |
|
1463 |
* index specified by the <code>end</code> parameter. |
|
1464 |
* |
|
1465 |
* <p>Depending on the transparency and anchoring being used (see |
|
1466 |
* {@link #useTransparentBounds useTransparentBounds} and |
|
1467 |
* {@link #useAnchoringBounds useAnchoringBounds}), certain constructs such |
|
1468 |
* as anchors may behave differently at or around the boundaries of the |
|
1469 |
* region. |
|
1470 |
* |
|
1471 |
* @param start |
|
1472 |
* The index to start searching at (inclusive) |
|
1473 |
* @param end |
|
1474 |
* The index to end searching at (exclusive) |
|
1475 |
* @throws IndexOutOfBoundsException |
|
1476 |
* If start or end is less than zero, if |
|
1477 |
* start is greater than the length of the input sequence, if |
|
1478 |
* end is greater than the length of the input sequence, or if |
|
1479 |
* start is greater than end. |
|
1480 |
* @return this matcher |
|
1481 |
* @since 1.5 |
|
1482 |
*/ |
|
1483 |
public Matcher region(int start, int end) { |
|
1484 |
if ((start < 0) || (start > getTextLength())) |
|
1485 |
throw new IndexOutOfBoundsException("start"); |
|
1486 |
if ((end < 0) || (end > getTextLength())) |
|
1487 |
throw new IndexOutOfBoundsException("end"); |
|
1488 |
if (start > end) |
|
1489 |
throw new IndexOutOfBoundsException("start > end"); |
|
1490 |
reset(); |
|
1491 |
from = start; |
|
1492 |
to = end; |
|
1493 |
return this; |
|
1494 |
} |
|
1495 |
||
1496 |
/** |
|
1497 |
* Reports the start index of this matcher's region. The |
|
1498 |
* searches this matcher conducts are limited to finding matches |
|
1499 |
* within {@link #regionStart regionStart} (inclusive) and |
|
1500 |
* {@link #regionEnd regionEnd} (exclusive). |
|
1501 |
* |
|
1502 |
* @return The starting point of this matcher's region |
|
1503 |
* @since 1.5 |
|
1504 |
*/ |
|
1505 |
public int regionStart() { |
|
1506 |
return from; |
|
1507 |
} |
|
1508 |
||
1509 |
/** |
|
1510 |
* Reports the end index (exclusive) of this matcher's region. |
|
1511 |
* The searches this matcher conducts are limited to finding matches |
|
1512 |
* within {@link #regionStart regionStart} (inclusive) and |
|
1513 |
* {@link #regionEnd regionEnd} (exclusive). |
|
1514 |
* |
|
1515 |
* @return the ending point of this matcher's region |
|
1516 |
* @since 1.5 |
|
1517 |
*/ |
|
1518 |
public int regionEnd() { |
|
1519 |
return to; |
|
1520 |
} |
|
1521 |
||
1522 |
/** |
|
1523 |
* Queries the transparency of region bounds for this matcher. |
|
1524 |
* |
|
1525 |
* <p> This method returns <tt>true</tt> if this matcher uses |
|
1526 |
* <i>transparent</i> bounds, <tt>false</tt> if it uses <i>opaque</i> |
|
1527 |
* bounds. |
|
1528 |
* |
|
1529 |
* <p> See {@link #useTransparentBounds useTransparentBounds} for a |
|
1530 |
* description of transparent and opaque bounds. |
|
1531 |
* |
|
1532 |
* <p> By default, a matcher uses opaque region boundaries. |
|
1533 |
* |
|
1534 |
* @return <tt>true</tt> iff this matcher is using transparent bounds, |
|
1535 |
* <tt>false</tt> otherwise. |
|
1536 |
* @see java.util.regex.Matcher#useTransparentBounds(boolean) |
|
1537 |
* @since 1.5 |
|
1538 |
*/ |
|
1539 |
public boolean hasTransparentBounds() { |
|
1540 |
return transparentBounds; |
|
1541 |
} |
|
1542 |
||
1543 |
/** |
|
1544 |
* Sets the transparency of region bounds for this matcher. |
|
1545 |
* |
|
1546 |
* <p> Invoking this method with an argument of <tt>true</tt> will set this |
|
1547 |
* matcher to use <i>transparent</i> bounds. If the boolean |
|
1548 |
* argument is <tt>false</tt>, then <i>opaque</i> bounds will be used. |
|
1549 |
* |
|
1550 |
* <p> Using transparent bounds, the boundaries of this |
|
1551 |
* matcher's region are transparent to lookahead, lookbehind, |
|
1552 |
* and boundary matching constructs. Those constructs can see beyond the |
|
1553 |
* boundaries of the region to see if a match is appropriate. |
|
1554 |
* |
|
1555 |
* <p> Using opaque bounds, the boundaries of this matcher's |
|
1556 |
* region are opaque to lookahead, lookbehind, and boundary matching |
|
1557 |
* constructs that may try to see beyond them. Those constructs cannot |
|
1558 |
* look past the boundaries so they will fail to match anything outside |
|
1559 |
* of the region. |
|
1560 |
* |
|
1561 |
* <p> By default, a matcher uses opaque bounds. |
|
1562 |
* |
|
1563 |
* @param b a boolean indicating whether to use opaque or transparent |
|
1564 |
* regions |
|
1565 |
* @return this matcher |
|
1566 |
* @see java.util.regex.Matcher#hasTransparentBounds |
|
1567 |
* @since 1.5 |
|
1568 |
*/ |
|
1569 |
public Matcher useTransparentBounds(boolean b) { |
|
1570 |
transparentBounds = b; |
|
1571 |
return this; |
|
1572 |
} |
|
1573 |
||
1574 |
/** |
|
1575 |
* Queries the anchoring of region bounds for this matcher. |
|
1576 |
* |
|
1577 |
* <p> This method returns <tt>true</tt> if this matcher uses |
|
1578 |
* <i>anchoring</i> bounds, <tt>false</tt> otherwise. |
|
1579 |
* |
|
1580 |
* <p> See {@link #useAnchoringBounds useAnchoringBounds} for a |
|
1581 |
* description of anchoring bounds. |
|
1582 |
* |
|
1583 |
* <p> By default, a matcher uses anchoring region boundaries. |
|
1584 |
* |
|
1585 |
* @return <tt>true</tt> iff this matcher is using anchoring bounds, |
|
1586 |
* <tt>false</tt> otherwise. |
|
1587 |
* @see java.util.regex.Matcher#useAnchoringBounds(boolean) |
|
1588 |
* @since 1.5 |
|
1589 |
*/ |
|
1590 |
public boolean hasAnchoringBounds() { |
|
1591 |
return anchoringBounds; |
|
1592 |
} |
|
1593 |
||
1594 |
/** |
|
1595 |
* Sets the anchoring of region bounds for this matcher. |
|
1596 |
* |
|
1597 |
* <p> Invoking this method with an argument of <tt>true</tt> will set this |
|
1598 |
* matcher to use <i>anchoring</i> bounds. If the boolean |
|
1599 |
* argument is <tt>false</tt>, then <i>non-anchoring</i> bounds will be |
|
1600 |
* used. |
|
1601 |
* |
|
1602 |
* <p> Using anchoring bounds, the boundaries of this |
|
1603 |
* matcher's region match anchors such as ^ and $. |
|
1604 |
* |
|
1605 |
* <p> Without anchoring bounds, the boundaries of this |
|
1606 |
* matcher's region will not match anchors such as ^ and $. |
|
1607 |
* |
|
1608 |
* <p> By default, a matcher uses anchoring region boundaries. |
|
1609 |
* |
|
1610 |
* @param b a boolean indicating whether or not to use anchoring bounds. |
|
1611 |
* @return this matcher |
|
1612 |
* @see java.util.regex.Matcher#hasAnchoringBounds |
|
1613 |
* @since 1.5 |
|
1614 |
*/ |
|
1615 |
public Matcher useAnchoringBounds(boolean b) { |
|
1616 |
anchoringBounds = b; |
|
1617 |
return this; |
|
1618 |
} |
|
1619 |
||
1620 |
/** |
|
1621 |
* <p>Returns the string representation of this matcher. The |
|
1622 |
* string representation of a <code>Matcher</code> contains information |
|
1623 |
* that may be useful for debugging. The exact format is unspecified. |
|
1624 |
* |
|
1625 |
* @return The string representation of this matcher |
|
1626 |
* @since 1.5 |
|
1627 |
*/ |
|
1628 |
public String toString() { |
|
1629 |
StringBuilder sb = new StringBuilder(); |
|
1630 |
sb.append("java.util.regex.Matcher"); |
|
1631 |
sb.append("[pattern=" + pattern()); |
|
1632 |
sb.append(" region="); |
|
1633 |
sb.append(regionStart() + "," + regionEnd()); |
|
1634 |
sb.append(" lastmatch="); |
|
1635 |
if ((first >= 0) && (group() != null)) { |
|
1636 |
sb.append(group()); |
|
1637 |
} |
|
1638 |
sb.append("]"); |
|
1639 |
return sb.toString(); |
|
1640 |
} |
|
1641 |
||
1642 |
/** |
|
1643 |
* <p>Returns true if the end of input was hit by the search engine in |
|
1644 |
* the last match operation performed by this matcher. |
|
1645 |
* |
|
1646 |
* <p>When this method returns true, then it is possible that more input |
|
1647 |
* would have changed the result of the last search. |
|
1648 |
* |
|
1649 |
* @return true iff the end of input was hit in the last match; false |
|
1650 |
* otherwise |
|
1651 |
* @since 1.5 |
|
1652 |
*/ |
|
1653 |
public boolean hitEnd() { |
|
1654 |
return hitEnd; |
|
1655 |
} |
|
1656 |
||
1657 |
/** |
|
1658 |
* <p>Returns true if more input could change a positive match into a |
|
1659 |
* negative one. |
|
1660 |
* |
|
1661 |
* <p>If this method returns true, and a match was found, then more |
|
1662 |
* input could cause the match to be lost. If this method returns false |
|
1663 |
* and a match was found, then more input might change the match but the |
|
1664 |
* match won't be lost. If a match was not found, then requireEnd has no |
|
1665 |
* meaning. |
|
1666 |
* |
|
1667 |
* @return true iff more input could change a positive match into a |
|
1668 |
* negative one. |
|
1669 |
* @since 1.5 |
|
1670 |
*/ |
|
1671 |
public boolean requireEnd() { |
|
1672 |
return requireEnd; |
|
1673 |
} |
|
1674 |
||
1675 |
/** |
|
1676 |
* Initiates a search to find a Pattern within the given bounds. |
|
1677 |
* The groups are filled with default values and the match of the root |
|
1678 |
* of the state machine is called. The state machine will hold the state |
|
1679 |
* of the match as it proceeds in this matcher. |
|
1680 |
* |
|
1681 |
* Matcher.from is not set here, because it is the "hard" boundary |
|
1682 |
* of the start of the search which anchors will set to. The from param |
|
1683 |
* is the "soft" boundary of the start of the search, meaning that the |
|
1684 |
* regex tries to match at that index but ^ won't match there. Subsequent |
|
1685 |
* calls to the search methods start at a new "soft" boundary which is |
|
1686 |
* the end of the previous match. |
|
1687 |
*/ |
|
1688 |
boolean search(int from) { |
|
1689 |
this.hitEnd = false; |
|
1690 |
this.requireEnd = false; |
|
1691 |
from = from < 0 ? 0 : from; |
|
1692 |
this.first = from; |
|
1693 |
this.oldLast = oldLast < 0 ? from : oldLast; |
|
1694 |
for (int i = 0; i < groups.length; i++) |
|
1695 |
groups[i] = -1; |
|
1696 |
acceptMode = NOANCHOR; |
|
1697 |
boolean result = parentPattern.root.match(this, from, text); |
|
1698 |
if (!result) |
|
1699 |
this.first = -1; |
|
1700 |
this.oldLast = this.last; |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1701 |
this.modCount++; |
2 | 1702 |
return result; |
1703 |
} |
|
1704 |
||
1705 |
/** |
|
1706 |
* Initiates a search for an anchored match to a Pattern within the given |
|
1707 |
* bounds. The groups are filled with default values and the match of the |
|
1708 |
* root of the state machine is called. The state machine will hold the |
|
1709 |
* state of the match as it proceeds in this matcher. |
|
1710 |
*/ |
|
1711 |
boolean match(int from, int anchor) { |
|
1712 |
this.hitEnd = false; |
|
1713 |
this.requireEnd = false; |
|
1714 |
from = from < 0 ? 0 : from; |
|
1715 |
this.first = from; |
|
1716 |
this.oldLast = oldLast < 0 ? from : oldLast; |
|
1717 |
for (int i = 0; i < groups.length; i++) |
|
1718 |
groups[i] = -1; |
|
1719 |
acceptMode = anchor; |
|
1720 |
boolean result = parentPattern.matchRoot.match(this, from, text); |
|
1721 |
if (!result) |
|
1722 |
this.first = -1; |
|
1723 |
this.oldLast = this.last; |
|
29243
80ea8d3d39d0
8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents:
25859
diff
changeset
|
1724 |
this.modCount++; |
2 | 1725 |
return result; |
1726 |
} |
|
1727 |
||
1728 |
/** |
|
1729 |
* Returns the end index of the text. |
|
1730 |
* |
|
1731 |
* @return the index after the last character in the text |
|
1732 |
*/ |
|
1733 |
int getTextLength() { |
|
1734 |
return text.length(); |
|
1735 |
} |
|
1736 |
||
1737 |
/** |
|
1738 |
* Generates a String from this Matcher's input in the specified range. |
|
1739 |
* |
|
1740 |
* @param beginIndex the beginning index, inclusive |
|
1741 |
* @param endIndex the ending index, exclusive |
|
1742 |
* @return A String generated from this Matcher's input |
|
1743 |
*/ |
|
1744 |
CharSequence getSubSequence(int beginIndex, int endIndex) { |
|
1745 |
return text.subSequence(beginIndex, endIndex); |
|
1746 |
} |
|
1747 |
||
1748 |
/** |
|
1749 |
* Returns this Matcher's input character at index i. |
|
1750 |
* |
|
1751 |
* @return A char from the specified index |
|
1752 |
*/ |
|
1753 |
char charAt(int i) { |
|
1754 |
return text.charAt(i); |
|
1755 |
} |
|
1756 |
||
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1757 |
/** |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1758 |
* Returns the group index of the matched capturing group. |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1759 |
* |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1760 |
* @return the index of the named-capturing group |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1761 |
*/ |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1762 |
int getMatchedGroupIndex(String name) { |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1763 |
Objects.requireNonNull(name, "Group name"); |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1764 |
if (first < 0) |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1765 |
throw new IllegalStateException("No match found"); |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1766 |
if (!parentPattern.namedGroups().containsKey(name)) |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1767 |
throw new IllegalArgumentException("No group with name <" + name + ">"); |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1768 |
return parentPattern.namedGroups().get(name); |
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
14342
diff
changeset
|
1769 |
} |
2 | 1770 |
} |