1 /* Matcher.java -- Instance of a regular expression applied to a char sequence.
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java
.util
.regex
;
41 import gnu
.regexp
.REMatch
;
44 * Instance of a regular expression applied to a char sequence.
48 public final class Matcher
50 private Pattern pattern
;
51 private CharSequence input
;
53 private int appendPosition
;
54 private REMatch match
;
56 Matcher(Pattern pattern
, CharSequence input
)
58 this.pattern
= pattern
;
63 * @param sb The target string buffer
64 * @param replacement The replacement string
66 * @exception IllegalStateException If no match has yet been attempted,
67 * or if the previous match operation failed
68 * @exception IndexOutOfBoundsException If the replacement string refers
69 * to a capturing group that does not exist in the pattern
71 public Matcher
appendReplacement (StringBuffer sb
, String replacement
)
72 throws IllegalStateException
75 sb
.append(input
.subSequence(appendPosition
,
76 match
.getStartIndex()).toString());
77 sb
.append(match
.substituteInto(replacement
));
78 appendPosition
= match
.getEndIndex();
83 * @param sb The target string buffer
85 public StringBuffer
appendTail (StringBuffer sb
)
87 sb
.append(input
.subSequence(appendPosition
, input
.length()).toString());
92 * @exception IllegalStateException If no match has yet been attempted,
93 * or if the previous match operation failed
96 throws IllegalStateException
99 return match
.getEndIndex();
103 * @param group The index of a capturing group in this matcher's pattern
105 * @exception IllegalStateException If no match has yet been attempted,
106 * or if the previous match operation failed
107 * @exception IndexOutOfBoundsException If the replacement string refers
108 * to a capturing group that does not exist in the pattern
110 public int end (int group
)
111 throws IllegalStateException
114 return match
.getEndIndex(group
);
117 public boolean find ()
119 boolean first
= (match
== null);
120 match
= pattern
.getRE().getMatch(input
, position
);
123 int endIndex
= match
.getEndIndex();
124 // Are we stuck at the same position?
125 if (!first
&& endIndex
== position
)
128 // Not at the end of the input yet?
129 if (position
< input
.length() - 1)
132 return find(position
);
144 * @param start The index to start the new pattern matching
146 * @exception IndexOutOfBoundsException If the replacement string refers
147 * to a capturing group that does not exist in the pattern
149 public boolean find (int start
)
151 match
= pattern
.getRE().getMatch(input
, start
);
154 position
= match
.getEndIndex();
161 * @exception IllegalStateException If no match has yet been attempted,
162 * or if the previous match operation failed
164 public String
group ()
167 return match
.toString();
171 * @param group The index of a capturing group in this matcher's pattern
173 * @exception IllegalStateException If no match has yet been attempted,
174 * or if the previous match operation failed
175 * @exception IndexOutOfBoundsException If the replacement string refers
176 * to a capturing group that does not exist in the pattern
178 public String
group (int group
)
179 throws IllegalStateException
182 return match
.toString(group
);
186 * @param replacement The replacement string
188 public String
replaceFirst (String replacement
)
191 // Semantics might not quite match
192 return pattern
.getRE().substitute(input
, replacement
, position
);
196 * @param replacement The replacement string
198 public String
replaceAll (String replacement
)
201 return pattern
.getRE().substituteAll(input
, replacement
, position
);
204 public int groupCount ()
206 return pattern
.getRE().getNumSubs();
209 public boolean lookingAt ()
211 match
= pattern
.getRE().getMatch(input
, 0);
214 if (match
.getStartIndex() == 0)
216 position
= match
.getEndIndex();
225 * Attempts to match the entire input sequence against the pattern.
227 * If the match succeeds then more information can be obtained via the
228 * start, end, and group methods.
234 public boolean matches ()
238 if (position
== input
.length())
246 * Returns the Pattern that is interpreted by this Matcher
248 public Pattern
pattern ()
253 public Matcher
reset ()
261 * @param input The new input character sequence
263 public Matcher
reset (CharSequence input
)
270 * @param group The index of a capturing group in this matcher's pattern
272 * @exception IllegalStateException If no match has yet been attempted,
273 * or if the previous match operation failed
276 throws IllegalStateException
279 return match
.getStartIndex();
283 * @param group The index of a capturing group in this matcher's pattern
285 * @exception IllegalStateException If no match has yet been attempted,
286 * or if the previous match operation failed
287 * @exception IndexOutOfBoundsException If the replacement string refers
288 * to a capturing group that does not exist in the pattern
290 public int start (int group
)
291 throws IllegalStateException
294 return match
.getStartIndex(group
);
297 private void assertMatchOp()
299 if (match
== null) throw new IllegalStateException();