2 * gnu/regexp/REMatch.java
3 * Copyright (C) 1998-2001 Wes Biggs
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 import java.io.Serializable;
24 * An instance of this class represents a match
25 * completed by a gnu.regexp matching function. It can be used
26 * to obtain relevant information about the location of a match
29 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
31 public final class REMatch implements Serializable, Cloneable {
32 private String matchedText;
34 // These variables are package scope for fast access within the engine
35 int eflags; // execution flags this match was made using
37 // Offset in source text where match was tried. This is zero-based;
38 // the actual position in the source text is given by (offset + anchor).
41 // Anchor position refers to the index into the source input
42 // at which the matching operation began.
43 // This is also useful for the ANCHORINDEX option.
46 // Package scope; used by RE.
47 int index; // used while matching to mark current match position in input
48 int[] start; // start positions (relative to offset) for each (sub)exp.
49 int[] end; // end positions for the same
50 REMatch next; // other possibility (to avoid having to use arrays)
52 public Object clone() {
54 REMatch copy = (REMatch) super.clone();
57 copy.start = (int[]) start.clone();
58 copy.end = (int[]) end.clone();
61 } catch (CloneNotSupportedException e) {
62 throw new Error(); // doesn't happen
66 void assignFrom(REMatch other) {
70 // need to deep clone?
74 REMatch(int subs, int anchor, int eflags) {
75 start = new int[subs+1];
76 end = new int[subs+1];
82 void finish(CharIndexed text) {
84 StringBuffer sb = new StringBuffer();
86 for (i = 0; i < end[0]; i++)
87 sb.append(text.charAt(i));
88 matchedText = sb.toString();
89 for (i = 0; i < start.length; i++) {
90 // If any subexpressions didn't terminate, they don't count
91 // TODO check if this code ever gets hit
92 if ((start[i] == -1) ^ (end[i] == -1)) {
97 next = null; // cut off alternates
100 /** Clears the current match and moves the offset to the new index. */
101 void clear(int index) {
104 for (int i = 0; i < start.length; i++) {
105 start[i] = end[i] = -1;
107 next = null; // cut off alternates
111 * Returns the string matching the pattern. This makes it convenient
112 * to write code like the following:
115 * REMatch myMatch = myExpression.getMatch(myString);<br>
116 * if (myMatch != null) System.out.println("Regexp found: "+myMatch);
119 public String toString() {
124 * Returns the index within the input text where the match in its entirety
127 public int getStartIndex() {
128 return offset + start[0];
132 * Returns the index within the input string where the match in
133 * its entirety ends. The return value is the next position after
134 * the end of the string; therefore, a match created by the
138 * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
140 * can be viewed (given that myMatch is not null) by creating
142 * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
143 * myMatch.getEndIndex());</code>
145 * But you can save yourself that work, since the <code>toString()</code>
146 * method (above) does exactly that for you.
148 public int getEndIndex() {
149 return offset + end[0];
153 * Returns the string matching the given subexpression. The subexpressions
154 * are indexed starting with one, not zero. That is, the subexpression
155 * identified by the first set of parentheses in a regular expression
156 * could be retrieved from an REMatch by calling match.toString(1).
158 * @param sub Index of the subexpression.
160 public String toString(int sub) {
161 if ((sub >= start.length) || (start[sub] == -1)) return "";
162 return (matchedText.substring(start[sub],end[sub]));
166 * Returns the index within the input string used to generate this match
167 * where subexpression number <i>sub</i> begins, or <code>-1</code> if
168 * the subexpression does not exist. The initial position is zero.
170 * @param sub Subexpression index
171 * @deprecated Use getStartIndex(int) instead.
173 public int getSubStartIndex(int sub) {
174 if (sub >= start.length) return -1;
176 return (x == -1) ? x : offset + x;
180 * Returns the index within the input string used to generate this match
181 * where subexpression number <i>sub</i> begins, or <code>-1</code> if
182 * the subexpression does not exist. The initial position is zero.
184 * @param sub Subexpression index
185 * @since gnu.regexp 1.1.0
187 public int getStartIndex(int sub) {
188 if (sub >= start.length) return -1;
190 return (x == -1) ? x : offset + x;
194 * Returns the index within the input string used to generate this match
195 * where subexpression number <i>sub</i> ends, or <code>-1</code> if
196 * the subexpression does not exist. The initial position is zero.
198 * @param sub Subexpression index
199 * @deprecated Use getEndIndex(int) instead
201 public int getSubEndIndex(int sub) {
202 if (sub >= start.length) return -1;
204 return (x == -1) ? x : offset + x;
208 * Returns the index within the input string used to generate this match
209 * where subexpression number <i>sub</i> ends, or <code>-1</code> if
210 * the subexpression does not exist. The initial position is zero.
212 * @param sub Subexpression index
214 public int getEndIndex(int sub) {
215 if (sub >= start.length) return -1;
217 return (x == -1) ? x : offset + x;
221 * Substitute the results of this match to create a new string.
222 * This is patterned after PERL, so the tokens to watch out for are
223 * <code>$0</code> through <code>$9</code>. <code>$0</code> matches
224 * the full substring matched; <code>$<i>n</i></code> matches
225 * subexpression number <i>n</i>.
227 * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
229 public String substituteInto(String input) {
230 // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
231 StringBuffer output = new StringBuffer();
233 for (pos = 0; pos < input.length()-1; pos++) {
234 if ((input.charAt(pos) == '$') && (Character.isDigit(input.charAt(pos+1)))) {
235 int val = Character.digit(input.charAt(++pos),10);
236 if (val < start.length) {
237 output.append(toString(val));
239 } else output.append(input.charAt(pos));
241 if (pos < input.length()) output.append(input.charAt(pos));
242 return output.toString();