1 /* Matcher.java -- Instance of a regular expression applied to a char sequence.
2 Copyright (C) 2002, 2004, 2006 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., 51 Franklin Street, Fifth Floor, 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;
42 import gnu.regexp.REMatch;
43 import gnu.regexp.CharIndexed;
46 * Instance of a regular expression applied to a char sequence.
50 public final class Matcher implements MatchResult
52 private Pattern pattern;
53 private CharSequence input;
54 // We use CharIndexed as an input object to the getMatch method in order
55 // that /\G/ (the end of the previous match) may work. The information
56 // of the previous match is stored in the CharIndexed object.
57 private CharIndexed inputCharIndexed;
59 private int appendPosition;
60 private REMatch match;
62 Matcher(Pattern pattern, CharSequence input)
64 this.pattern = pattern;
66 this.inputCharIndexed = RE.makeCharIndexed(input, 0);
70 * @param sb The target string buffer
71 * @param replacement The replacement string
73 * @exception IllegalStateException If no match has yet been attempted,
74 * or if the previous match operation failed
75 * @exception IndexOutOfBoundsException If the replacement string refers
76 * to a capturing group that does not exist in the pattern
78 public Matcher appendReplacement (StringBuffer sb, String replacement)
79 throws IllegalStateException
82 sb.append(input.subSequence(appendPosition,
83 match.getStartIndex()).toString());
84 sb.append(RE.getReplacement(replacement, match,
85 RE.REG_REPLACE_USE_BACKSLASHESCAPE));
86 appendPosition = match.getEndIndex();
91 * @param sb The target string buffer
93 public StringBuffer appendTail (StringBuffer sb)
95 sb.append(input.subSequence(appendPosition, input.length()).toString());
100 * @exception IllegalStateException If no match has yet been attempted,
101 * or if the previous match operation failed
104 throws IllegalStateException
107 return match.getEndIndex();
111 * @param group The index of a capturing group in this matcher's pattern
113 * @exception IllegalStateException If no match has yet been attempted,
114 * or if the previous match operation failed
115 * @exception IndexOutOfBoundsException If the replacement string refers
116 * to a capturing group that does not exist in the pattern
118 public int end (int group)
119 throws IllegalStateException
122 return match.getEndIndex(group);
125 public boolean find ()
127 boolean first = (match == null);
128 match = pattern.getRE().getMatch(inputCharIndexed, position);
131 int endIndex = match.getEndIndex();
132 // Are we stuck at the same position?
133 if (!first && endIndex == position)
136 // Not at the end of the input yet?
137 if (position < input.length() - 1)
140 return find(position);
152 * @param start The index to start the new pattern matching
154 * @exception IndexOutOfBoundsException If the replacement string refers
155 * to a capturing group that does not exist in the pattern
157 public boolean find (int start)
159 match = pattern.getRE().getMatch(inputCharIndexed, start);
162 position = match.getEndIndex();
169 * @exception IllegalStateException If no match has yet been attempted,
170 * or if the previous match operation failed
172 public String group ()
175 return match.toString();
179 * @param group The index of a capturing group in this matcher's pattern
181 * @exception IllegalStateException If no match has yet been attempted,
182 * or if the previous match operation failed
183 * @exception IndexOutOfBoundsException If the replacement string refers
184 * to a capturing group that does not exist in the pattern
186 public String group (int group)
187 throws IllegalStateException
190 return match.toString(group);
194 * @param replacement The replacement string
196 public String replaceFirst (String replacement)
199 // Semantics might not quite match
200 return pattern.getRE().substitute(input, replacement, position,
201 RE.REG_REPLACE_USE_BACKSLASHESCAPE);
205 * @param replacement The replacement string
207 public String replaceAll (String replacement)
210 return pattern.getRE().substituteAll(input, replacement, position,
211 RE.REG_REPLACE_USE_BACKSLASHESCAPE);
214 public int groupCount ()
216 return pattern.getRE().getNumSubs();
219 public boolean lookingAt ()
221 match = pattern.getRE().getMatch(inputCharIndexed, 0);
224 if (match.getStartIndex() == 0)
226 position = match.getEndIndex();
235 * Attempts to match the entire input sequence against the pattern.
237 * If the match succeeds then more information can be obtained via the
238 * start, end, and group methods.
244 public boolean matches ()
246 match = pattern.getRE().getMatch(inputCharIndexed, 0, RE.REG_TRY_ENTIRE_MATCH);
249 if (match.getStartIndex() == 0)
251 position = match.getEndIndex();
252 if (position == input.length())
261 * Returns the Pattern that is interpreted by this Matcher
263 public Pattern pattern ()
268 public Matcher reset ()
276 * @param input The new input character sequence
278 public Matcher reset (CharSequence input)
285 * @returns the index of a capturing group in this matcher's pattern
287 * @exception IllegalStateException If no match has yet been attempted,
288 * or if the previous match operation failed
291 throws IllegalStateException
294 return match.getStartIndex();
298 * @param group The index of a capturing group in this matcher's pattern
300 * @exception IllegalStateException If no match has yet been attempted,
301 * or if the previous match operation failed
302 * @exception IndexOutOfBoundsException If the replacement string refers
303 * to a capturing group that does not exist in the pattern
305 public int start (int group)
306 throws IllegalStateException
309 return match.getStartIndex(group);
312 private void assertMatchOp()
314 if (match == null) throw new IllegalStateException();