2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
11 Shareware: package pat
12 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
15 * This class is used to store a result from Regex
17 public class RegRes implements Cloneable
19 protected int[] marks = null;
21 protected boolean didMatch_ = false;
23 protected StringLike src = null;
25 /** Obtain the text String that was matched against. */
26 public String getString()
28 return src.toString();
31 /** Obtain the source StringLike object. */
32 public StringLike getStringLike()
37 protected int charsMatched_ = 0, matchFrom_ = 0, numSubs_ = 0;
39 public String toString()
41 StringBuffer sb = new StringBuffer();
42 sb.append("match=" + matchedFrom() + ":" + charsMatched());
47 for (int i = 0; i < numSubs(); i++)
51 " sub(" + n + ")=" + matchedFrom(n) + ":" + charsMatched(n));
60 public RegRes(RegRes r)
65 public void copyOutOf(RegRes r)
75 // marks = (Hashtable)r.marks.clone();
76 marks = new int[r.marks.length];
77 for (int i = 0; i < marks.length; i++)
79 marks[i] = r.marks[i];
81 // marks = (int[])r.marks.clone();
86 didMatch_ = r.didMatch_;
88 charsMatched_ = r.charsMatched_;
89 matchFrom_ = r.matchFrom_;
90 numSubs_ = r.numSubs_;
95 return new RegRes(this);
98 public boolean equals(RegRes r)
100 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
101 || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
102 || !src.unwrap().equals(r.src.unwrap()))
106 if (marks == null && r.marks != null)
110 if (marks != null && r.marks == null)
114 for (int i = 1; i <= numSubs_; i++)
116 if (matchedFrom(i) != r.matchedFrom(i))
120 else if (charsMatched(i) != r.charsMatched(i))
128 /** Obtains the match if successful, null otherwise. */
129 public String stringMatched()
131 int mf = matchedFrom(), cm = charsMatched();
132 return !didMatch_ || mf < 0 || cm < 0 ? null
133 : src.substring(mf, mf + cm);
137 * Obtains the position backreference number i begins to match, or -1 if
138 * backreference i was not matched.
140 public int matchedFrom(int i)
142 if (marks == null || i > numSubs_)
146 // Integer in=(Integer)marks.get("left"+i);
147 // return in == null ? -1 : in.intValue();
152 * Obtains the number of characters matched by backreference i, or -1 if
153 * backreference i was not matched.
155 public int charsMatched(int i)
157 if (marks == null || i > numSubs_ || !didMatch_)
161 // Integer in = (Integer)marks.get("right"+i);
162 // int i2 = in==null ? -1 : in.intValue();
163 int mf = matchedFrom(i);
164 return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);
168 * This is either equal to matchedFrom(i)+charsMatched(i) if the match was
169 * successful, or -1 if it was not.
171 public int matchedTo(int i)
173 if (marks == null || i > numSubs_ || !didMatch_)
177 return marks[i + numSubs_];
181 * Obtains a substring matching the nth set of parenthesis from the pattern.
182 * See numSubs(void), or null if the nth backrefence did not match.
184 public String stringMatched(int i)
186 int mf = matchedFrom(i), cm = charsMatched(i);
187 return !didMatch_ || mf < 0 || cm < 0 ? null
188 : src.substring(mf, mf + cm);
192 * This returns the part of the string that preceeds the match, or null if the
197 int mf = matchedFrom();
198 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
202 * This returns the part of the string that follows the ith backreference, or
203 * null if the backreference did not match.
205 public String left(int i)
207 int mf = matchedFrom(i);
208 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
212 * This returns the part of the string that follows the match, or null if the
213 * backreference did not match.
215 public String right()
217 int mf = matchedFrom(), cm = charsMatched();
218 return !didMatch_ || mf < 0 || cm < 0 ? null
219 : src.substring(mf + cm, src.length());
223 * This returns the string to the right of the ith backreference, or null if
224 * the backreference did not match.
226 public String right(int i)
228 int mf = matchedFrom(i), cm = charsMatched(i);
229 return !didMatch_ || mf < 0 || cm < 0 ? null
230 : src.substring(mf + cm, src.length());
234 * After a successful match, this returns the location of the first matching
235 * character, or -1 if the match failed.
237 public int matchedFrom()
239 return !didMatch_ ? -1 : matchFrom_;
243 * After a successful match, this returns the number of characters in the
244 * match, or -1 if the match failed.
246 public int charsMatched()
248 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
252 * This is matchedFrom()+charsMatched() after a successful match, or -1
255 public int matchedTo()
257 return !didMatch_ ? -1 : matchFrom_ + charsMatched_;
261 * This returns the number of backreferences (parenthesis) in the pattern,
262 * i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc.
269 /** Contains true if the last match was successful. */
270 public boolean didMatch()
275 /** An older name for matchedFrom. */
276 public int matchFrom()
278 return matchedFrom();
281 /** An older name for stringMatched(). */
282 public String substring()
284 return stringMatched();
287 /** An older name for matchedFrom. */
288 public int matchFrom(int i)
290 return matchedFrom(i);
293 /** An older name for stringMatched. */
294 public String substring(int i)
296 return stringMatched(i);