2 // This software is now distributed according to
\r
3 // the Lesser Gnu Public License. Please see
\r
4 // http://www.gnu.org/copyleft/lesser.txt for
\r
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\r
11 Shareware: package pat
\r
12 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
\r
15 * This class is used to store a result from Regex
\r
17 public class RegRes implements Cloneable
\r
19 protected int[] marks = null;
\r
21 protected boolean didMatch_ = false;
\r
23 protected StringLike src = null;
\r
25 /** Obtain the text String that was matched against. */
\r
26 public String getString()
\r
28 return src.toString();
\r
31 /** Obtain the source StringLike object. */
\r
32 public StringLike getStringLike()
\r
37 protected int charsMatched_ = 0, matchFrom_ = 0, numSubs_ = 0;
\r
39 public String toString()
\r
41 javajs.util.SB sb = new javajs.util.SB();
\r
42 sb.append("match=" + matchedFrom() + ":" + charsMatched());
\r
45 return sb.toString();
\r
47 for (int i = 0; i < numSubs(); i++)
\r
51 .append(" sub(" + n + ")=" + matchedFrom(n) + ":"
\r
54 return sb.toString();
\r
61 public RegRes(RegRes r)
\r
66 public void copyOutOf(RegRes r)
\r
68 if (r.marks == null)
\r
76 // marks = (Hashtable)r.marks.clone();
\r
77 marks = new int[r.marks.length];
\r
78 for (int i = 0; i < marks.length; i++)
\r
80 marks[i] = r.marks[i];
\r
82 // marks = (int[])r.marks.clone();
\r
83 } catch (Throwable t)
\r
87 didMatch_ = r.didMatch_;
\r
89 charsMatched_ = r.charsMatched_;
\r
90 matchFrom_ = r.matchFrom_;
\r
91 numSubs_ = r.numSubs_;
\r
94 public Object clone()
\r
96 return new RegRes(this);
\r
99 public boolean equals(RegRes r)
\r
101 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
\r
102 || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
\r
103 || !src.unwrap().equals(r.src.unwrap()))
\r
107 if (marks == null && r.marks != null)
\r
111 if (marks != null && r.marks == null)
\r
115 for (int i = 1; i <= numSubs_; i++)
\r
117 if (matchedFrom(i) != r.matchedFrom(i))
\r
121 else if (charsMatched(i) != r.charsMatched(i))
\r
129 /** Obtains the match if successful, null otherwise. */
\r
130 public String stringMatched()
\r
132 int mf = matchedFrom(), cm = charsMatched();
\r
133 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
\r
138 * Obtains the position backreference number i begins to match, or -1 if
\r
139 * backreference i was not matched.
\r
141 public int matchedFrom(int i)
\r
143 if (marks == null || i > numSubs_)
\r
147 // Integer in=(Integer)marks.get("left"+i);
\r
148 // return in == null ? -1 : in.intValue();
\r
153 * Obtains the number of characters matched by backreference i, or -1 if
\r
154 * backreference i was not matched.
\r
156 public int charsMatched(int i)
\r
158 if (marks == null || i > numSubs_ || !didMatch_)
\r
162 // Integer in = (Integer)marks.get("right"+i);
\r
163 // int i2 = in==null ? -1 : in.intValue();
\r
164 int mf = matchedFrom(i);
\r
165 return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);
\r
169 * This is either equal to matchedFrom(i)+charsMatched(i) if the match was
\r
170 * successful, or -1 if it was not.
\r
172 public int matchedTo(int i)
\r
174 if (marks == null || i > numSubs_ || !didMatch_)
\r
178 return marks[i + numSubs_];
\r
182 * Obtains a substring matching the nth set of parenthesis from the pattern.
\r
183 * See numSubs(void), or null if the nth backrefence did not match.
\r
185 public String stringMatched(int i)
\r
187 int mf = matchedFrom(i), cm = charsMatched(i);
\r
188 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
\r
193 * This returns the part of the string that preceeds the match, or null if the
\r
196 public String left()
\r
198 int mf = matchedFrom();
\r
199 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
\r
203 * This returns the part of the string that follows the ith backreference, or
\r
204 * null if the backreference did not match.
\r
206 public String left(int i)
\r
208 int mf = matchedFrom(i);
\r
209 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
\r
213 * This returns the part of the string that follows the match, or null if the
\r
214 * backreference did not match.
\r
216 public String right()
\r
218 int mf = matchedFrom(), cm = charsMatched();
\r
219 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
\r
224 * This returns the string to the right of the ith backreference, or null if
\r
225 * the backreference did not match.
\r
227 public String right(int i)
\r
229 int mf = matchedFrom(i), cm = charsMatched(i);
\r
230 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
\r
235 * After a successful match, this returns the location of the first matching
\r
236 * character, or -1 if the match failed.
\r
238 public int matchedFrom()
\r
240 return !didMatch_ ? -1 : matchFrom_;
\r
244 * After a successful match, this returns the number of characters in the
\r
245 * match, or -1 if the match failed.
\r
247 public int charsMatched()
\r
249 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
\r
253 * This is matchedFrom()+charsMatched() after a successful match, or -1
\r
256 public int matchedTo()
\r
258 return !didMatch_ ? -1 : matchFrom_ + charsMatched_;
\r
262 * This returns the number of backreferences (parenthesis) in the pattern,
\r
263 * i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc.
\r
265 public int numSubs()
\r
270 /** Contains true if the last match was successful. */
\r
271 public boolean didMatch()
\r
276 /** An older name for matchedFrom. */
\r
277 public int matchFrom()
\r
279 return matchedFrom();
\r
282 /** An older name for stringMatched(). */
\r
283 public String substring()
\r
285 return stringMatched();
\r
288 /** An older name for matchedFrom. */
\r
289 public int matchFrom(int i)
\r
291 return matchedFrom(i);
\r
294 /** An older name for stringMatched. */
\r
295 public String substring(int i)
\r
297 return stringMatched(i);
\r