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 implements Cloneable
\r
19 protected int[] marks = null;
\r
20 protected boolean didMatch_ = false;
\r
21 protected StringLike src = null;
\r
23 /** Obtain the text String that was matched against. */
\r
24 public String getString()
\r
26 return src.toString();
\r
29 /** Obtain the source StringLike object. */
\r
30 public StringLike getStringLike()
\r
35 protected int charsMatched_ = 0, matchFrom_ = 0, numSubs_ = 0;
\r
36 public String toString()
\r
38 StringBuffer sb = new StringBuffer();
\r
39 sb.append("match=" + matchedFrom() + ":" + charsMatched());
\r
42 return sb.toString();
\r
44 for (int i = 0; i < numSubs(); i++)
\r
47 sb.append(" sub(" + n + ")=" + matchedFrom(n) +
\r
48 ":" + charsMatched(n));
\r
50 return sb.toString();
\r
56 public RegRes(RegRes r)
\r
61 public void copyOutOf(RegRes r)
\r
63 if (r.marks == null)
\r
71 //marks = (Hashtable)r.marks.clone();
\r
72 marks = new int[r.marks.length];
\r
73 for (int i = 0; i < marks.length; i++)
\r
75 marks[i] = r.marks[i];
\r
77 //marks = (int[])r.marks.clone();
\r
82 didMatch_ = r.didMatch_;
\r
84 charsMatched_ = r.charsMatched_;
\r
85 matchFrom_ = r.matchFrom_;
\r
86 numSubs_ = r.numSubs_;
\r
89 public Object clone()
\r
91 return new RegRes(this);
\r
94 public boolean equals(RegRes r)
\r
96 if (charsMatched_ != r.charsMatched_
\r
97 || matchFrom_ != r.matchFrom_
\r
98 || didMatch_ != r.didMatch_
\r
99 || numSubs_ != r.numSubs_
\r
100 || !src.unwrap().equals(r.src.unwrap()))
\r
104 if (marks == null && r.marks != null)
\r
108 if (marks != null && r.marks == null)
\r
112 for (int i = 1; i <= numSubs_; i++)
\r
114 if (matchedFrom(i) != r.matchedFrom(i))
\r
118 else if (charsMatched(i) != r.charsMatched(i))
\r
126 /** Obtains the match if successful, null otherwise.*/
\r
127 public String stringMatched()
\r
129 int mf = matchedFrom(), cm = charsMatched();
\r
130 return!didMatch_ || mf < 0 || cm < 0 ? null :
\r
131 src.substring(mf, mf + cm);
\r
134 /** Obtains the position backreference number i begins to match, or
\r
135 -1 if backreference i was not matched. */
\r
136 public int matchedFrom(int i)
\r
138 if (marks == null || i > numSubs_)
\r
142 //Integer in=(Integer)marks.get("left"+i);
\r
143 //return in == null ? -1 : in.intValue();
\r
147 /** Obtains the number of characters matched by backreference i, or
\r
148 -1 if backreference i was not matched. */
\r
149 public int charsMatched(int i)
\r
151 if (marks == null || i > numSubs_ || !didMatch_)
\r
155 //Integer in = (Integer)marks.get("right"+i);
\r
156 //int i2 = in==null ? -1 : in.intValue();
\r
157 int mf = matchedFrom(i);
\r
158 return mf < 0 ? -1 : marks[i + numSubs_] - matchedFrom(i);
\r
161 /** This is either equal to matchedFrom(i)+charsMatched(i) if the match
\r
162 was successful, or -1 if it was not. */
\r
163 public int matchedTo(int i)
\r
165 if (marks == null || i > numSubs_ || !didMatch_)
\r
169 return marks[i + numSubs_];
\r
172 /** Obtains a substring matching the nth set
\r
173 of parenthesis from the pattern. See
\r
174 numSubs(void), or null if the nth backrefence did
\r
176 public String stringMatched(int i)
\r
178 int mf = matchedFrom(i), cm = charsMatched(i);
\r
179 return!didMatch_ || mf < 0 || cm < 0 ? null :
\r
180 src.substring(mf, mf + cm);
\r
183 /** This returns the part of the string that preceeds the match,
\r
184 or null if the match failed.*/
\r
185 public String left()
\r
187 int mf = matchedFrom();
\r
188 return!didMatch_ || (mf < 0) ? null : src.substring(0, mf);
\r
191 /** This returns the part of the string that follows the ith
\r
192 backreference, or null if the backreference did not match. */
\r
193 public String left(int i)
\r
195 int mf = matchedFrom(i);
\r
196 return!didMatch_ || (mf < 0) ? null : src.substring(0, mf);
\r
199 /** This returns the part of the string that follows the match,
\r
200 or null if the backreference did not match.*/
\r
201 public String right()
\r
203 int mf = matchedFrom(), cm = charsMatched();
\r
204 return!didMatch_ || mf < 0 || cm < 0 ? null : src.substring(mf +
\r
208 /** This returns the string to the right of the ith backreference,
\r
209 or null if the backreference did not match. */
\r
210 public String right(int i)
\r
212 int mf = matchedFrom(i), cm = charsMatched(i);
\r
213 return!didMatch_ || mf < 0 || cm < 0 ? null :
\r
214 src.substring(mf + cm, src.length());
\r
217 /** After a successful match, this returns the location of
\r
218 the first matching character, or -1 if the match failed.*/
\r
219 public int matchedFrom()
\r
221 return!didMatch_ ? -1 : matchFrom_;
\r
224 /** After a successful match, this returns the number of
\r
225 characters in the match, or -1 if the match failed. */
\r
226 public int charsMatched()
\r
228 return!didMatch_ || matchFrom_ < 0 ? -1 : charsMatched_;
\r
231 /** This is matchedFrom()+charsMatched() after a successful match,
\r
232 or -1 otherwise. */
\r
233 public int matchedTo()
\r
235 return!didMatch_ ? -1 : matchFrom_ + charsMatched_;
\r
238 /** This returns the number of
\r
239 backreferences (parenthesis) in the pattern,
\r
240 i.e. the pattern "(ab)" has
\r
241 one, the pattern "(a)(b)" has two, etc. */
\r
242 public int numSubs()
\r
247 /** Contains true if the last match was successful. */
\r
248 public boolean didMatch()
\r
253 /** An older name for matchedFrom. */
\r
254 public int matchFrom()
\r
256 return matchedFrom();
\r
259 /** An older name for stringMatched(). */
\r
260 public String substring()
\r
262 return stringMatched();
\r
265 /** An older name for matchedFrom. */
\r
266 public int matchFrom(int i)
\r
268 return matchedFrom(i);
\r
271 /** An older name for stringMatched. */
\r
272 public String substring(int i)
\r
274 return stringMatched(i);
\r