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++)
50 sb.append(" sub(" + n + ")=" + matchedFrom(n) + ":" + charsMatched(n));
59 public RegRes(RegRes r)
64 public void copyOutOf(RegRes r)
74 // marks = (Hashtable)r.marks.clone();
75 marks = new int[r.marks.length];
76 for (int i = 0; i < marks.length; i++)
78 marks[i] = r.marks[i];
80 // marks = (int[])r.marks.clone();
85 didMatch_ = r.didMatch_;
87 charsMatched_ = r.charsMatched_;
88 matchFrom_ = r.matchFrom_;
89 numSubs_ = r.numSubs_;
94 return new RegRes(this);
97 public boolean equals(RegRes r)
99 if (charsMatched_ != r.charsMatched_ || matchFrom_ != r.matchFrom_
100 || didMatch_ != r.didMatch_ || numSubs_ != r.numSubs_
101 || !src.unwrap().equals(r.src.unwrap()))
105 if (marks == null && r.marks != null)
109 if (marks != null && r.marks == null)
113 for (int i = 1; i <= numSubs_; i++)
115 if (matchedFrom(i) != r.matchedFrom(i))
119 else if (charsMatched(i) != r.charsMatched(i))
127 /** Obtains the match if successful, null otherwise. */
128 public String stringMatched()
130 int mf = matchedFrom(), cm = charsMatched();
131 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
136 * Obtains the position backreference number i begins to match, or -1 if
137 * backreference i was not matched.
139 public int matchedFrom(int i)
141 if (marks == null || i > numSubs_)
145 // Integer in=(Integer)marks.get("left"+i);
146 // return in == null ? -1 : in.intValue();
151 * Obtains the number of characters matched by backreference i, or -1 if
152 * backreference i was not matched.
154 public int charsMatched(int i)
156 if (marks == null || i > numSubs_ || !didMatch_)
160 // Integer in = (Integer)marks.get("right"+i);
161 // int i2 = in==null ? -1 : in.intValue();
162 int mf = matchedFrom(i);
163 return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);
167 * This is either equal to matchedFrom(i)+charsMatched(i) if the match was
168 * successful, or -1 if it was not.
170 public int matchedTo(int i)
172 if (marks == null || i > numSubs_ || !didMatch_)
176 return marks[i + numSubs_];
180 * Obtains a substring matching the nth set of parenthesis from the pattern.
181 * See numSubs(void), or null if the nth backrefence did not match.
183 public String stringMatched(int i)
185 int mf = matchedFrom(i), cm = charsMatched(i);
186 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf, mf
191 * This returns the part of the string that preceeds the match, or null if the
196 int mf = matchedFrom();
197 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
201 * This returns the part of the string that follows the ith backreference, or
202 * null if the backreference did not match.
204 public String left(int i)
206 int mf = matchedFrom(i);
207 return !didMatch_ || (mf < 0) ? null : src.substring(0, mf);
211 * This returns the part of the string that follows the match, or null if the
212 * backreference did not match.
214 public String right()
216 int mf = matchedFrom(), cm = charsMatched();
217 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
222 * This returns the string to the right of the ith backreference, or null if
223 * the backreference did not match.
225 public String right(int i)
227 int mf = matchedFrom(i), cm = charsMatched(i);
228 return !didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf + cm,
233 * After a successful match, this returns the location of the first matching
234 * character, or -1 if the match failed.
236 public int matchedFrom()
238 return !didMatch_ ? -1 : matchFrom_;
242 * After a successful match, this returns the number of characters in the
243 * match, or -1 if the match failed.
245 public int charsMatched()
247 return !didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
251 * This is matchedFrom()+charsMatched() after a successful match, or -1
254 public int matchedTo()
256 return !didMatch_ ? -1 : matchFrom_ + charsMatched_;
260 * This returns the number of backreferences (parenthesis) in the pattern,
261 * i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc.
268 /** Contains true if the last match was successful. */
269 public boolean didMatch()
274 /** An older name for matchedFrom. */
275 public int matchFrom()
277 return matchedFrom();
280 /** An older name for stringMatched(). */
281 public String substring()
283 return stringMatched();
286 /** An older name for matchedFrom. */
287 public int matchFrom(int i)
289 return matchedFrom(i);
292 /** An older name for stringMatched. */
293 public String substring(int i)
295 return stringMatched(i);