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 .append(" sub(" + n + ")=" + matchedFrom(n) + ":"
61 public RegRes(RegRes r)
66 public void copyOutOf(RegRes r)
76 // marks = (Hashtable)r.marks.clone();
77 marks = new int[r.marks.length];
78 for (int i = 0; i < marks.length; i++)
80 marks[i] = r.marks[i];
82 // marks = (int[])r.marks.clone();
87 didMatch_ = r.didMatch_;
89 charsMatched_ = r.charsMatched_;
90 matchFrom_ = r.matchFrom_;
91 numSubs_ = r.numSubs_;
96 return new RegRes(this);
99 public boolean equals(RegRes r)
101 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
102 || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
103 || !src.unwrap().equals(r.src.unwrap()))
107 if (marks == null && r.marks != null)
111 if (marks != null && r.marks == null)
115 for (int i = 1; i <= numSubs_; i++)
117 if (matchedFrom(i) != r.matchedFrom(i))
121 else if (charsMatched(i) != r.charsMatched(i))
129 /** Obtains the match if successful, null otherwise. */
130 public String stringMatched()
132 int mf = matchedFrom(), cm = charsMatched();
133 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
138 * Obtains the position backreference number i begins to match, or -1 if
139 * backreference i was not matched.
141 public int matchedFrom(int i)
143 if (marks == null || i > numSubs_)
147 // Integer in=(Integer)marks.get("left"+i);
148 // return in == null ? -1 : in.intValue();
153 * Obtains the number of characters matched by backreference i, or -1 if
154 * backreference i was not matched.
156 public int charsMatched(int i)
158 if (marks == null || i > numSubs_ || !didMatch_)
162 // Integer in = (Integer)marks.get("right"+i);
163 // int i2 = in==null ? -1 : in.intValue();
164 int mf = matchedFrom(i);
165 return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);
169 * This is either equal to matchedFrom(i)+charsMatched(i) if the match was
170 * successful, or -1 if it was not.
172 public int matchedTo(int i)
174 if (marks == null || i > numSubs_ || !didMatch_)
178 return marks[i + numSubs_];
182 * Obtains a substring matching the nth set of parenthesis from the pattern.
183 * See numSubs(void), or null if the nth backrefence did not match.
185 public String stringMatched(int i)
187 int mf = matchedFrom(i), cm = charsMatched(i);
188 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
193 * This returns the part of the string that preceeds the match, or null if the
198 int mf = matchedFrom();
199 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
203 * This returns the part of the string that follows the ith backreference, or
204 * null if the backreference did not match.
206 public String left(int i)
208 int mf = matchedFrom(i);
209 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
213 * This returns the part of the string that follows the match, or null if the
214 * backreference did not match.
216 public String right()
218 int mf = matchedFrom(), cm = charsMatched();
219 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
224 * This returns the string to the right of the ith backreference, or null if
225 * the backreference did not match.
227 public String right(int i)
229 int mf = matchedFrom(i), cm = charsMatched(i);
230 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
235 * After a successful match, this returns the location of the first matching
236 * character, or -1 if the match failed.
238 public int matchedFrom()
240 return !didMatch_ ? -1 : matchFrom_;
244 * After a successful match, this returns the number of characters in the
245 * match, or -1 if the match failed.
247 public int charsMatched()
249 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
253 * This is matchedFrom()+charsMatched() after a successful match, or -1
256 public int matchedTo()
258 return !didMatch_ ? -1 : matchFrom_ + charsMatched_;
262 * This returns the number of backreferences (parenthesis) in the pattern,
263 * i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc.
270 /** Contains true if the last match was successful. */
271 public boolean didMatch()
276 /** An older name for matchedFrom. */
277 public int matchFrom()
279 return matchedFrom();
282 /** An older name for stringMatched(). */
283 public String substring()
285 return stringMatched();
288 /** An older name for matchedFrom. */
289 public int matchFrom(int i)
291 return matchedFrom(i);
294 /** An older name for stringMatched. */
295 public String substring(int i)
297 return stringMatched(i);