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
10 Shareware: package pat
\r
11 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
\r
13 This class is used to store a result from Regex */
\r
14 public class RegRes implements Cloneable {
\r
15 protected int[] marks = null;
\r
16 protected boolean didMatch_ = false;
\r
17 protected StringLike src=null;
\r
19 /** Obtain the text String that was matched against. */
\r
20 public String getString() { return src.toString(); }
\r
21 /** Obtain the source StringLike object. */
\r
22 public StringLike getStringLike() { return src; }
\r
23 protected int charsMatched_=0,matchFrom_=0,numSubs_=0;
\r
24 public String toString() {
\r
25 StringBuffer sb = new StringBuffer();
\r
26 sb.append("match="+matchedFrom()+":"+charsMatched());
\r
27 if(!didMatch()) return sb.toString();
\r
28 for(int i=0;i<numSubs();i++) {
\r
30 sb.append(" sub("+n+")="+matchedFrom(n)+
\r
31 ":"+charsMatched(n));
\r
33 return sb.toString();
\r
36 public RegRes(RegRes r) {
\r
39 public void copyOutOf(RegRes r) {
\r
43 //marks = (Hashtable)r.marks.clone();
\r
44 marks = new int[r.marks.length];
\r
45 for(int i=0;i<marks.length;i++)
\r
46 marks[i]=r.marks[i];
\r
47 //marks = (int[])r.marks.clone();
\r
48 } catch (Throwable t) {}
\r
49 didMatch_ = r.didMatch_;
\r
51 charsMatched_ = r.charsMatched_;
\r
52 matchFrom_ = r.matchFrom_;
\r
53 numSubs_ = r.numSubs_;
\r
55 public Object clone() { return new RegRes(this); }
\r
56 public boolean equals(RegRes r) {
\r
57 if(charsMatched_!=r.charsMatched_
\r
58 || matchFrom_ !=r.matchFrom_
\r
59 || didMatch_ !=r.didMatch_
\r
60 || numSubs_ !=r.numSubs_
\r
61 || !src.unwrap().equals(r.src.unwrap()))
\r
63 if(marks==null && r.marks!=null)
\r
65 if(marks!=null && r.marks==null)
\r
67 for(int i=1;i<=numSubs_;i++) {
\r
68 if(matchedFrom(i) != r.matchedFrom(i))
\r
70 else if(charsMatched(i) != r.charsMatched(i))
\r
75 /** Obtains the match if successful, null otherwise.*/
\r
76 public String stringMatched() {
\r
77 int mf=matchedFrom(), cm = charsMatched();
\r
78 return !didMatch_ || mf<0 || cm<0 ? null :
\r
79 src.substring(mf,mf+cm);
\r
81 /** Obtains the position backreference number i begins to match, or
\r
82 -1 if backreference i was not matched. */
\r
83 public int matchedFrom(int i) {
\r
84 if(marks==null||i>numSubs_) return -1;
\r
85 //Integer in=(Integer)marks.get("left"+i);
\r
86 //return in == null ? -1 : in.intValue();
\r
89 /** Obtains the number of characters matched by backreference i, or
\r
90 -1 if backreference i was not matched. */
\r
91 public int charsMatched(int i) {
\r
92 if(marks==null||i>numSubs_||!didMatch_) return -1;
\r
93 //Integer in = (Integer)marks.get("right"+i);
\r
94 //int i2 = in==null ? -1 : in.intValue();
\r
95 int mf = matchedFrom(i);
\r
96 return mf < 0 ? -1 : marks[i+numSubs_]-matchedFrom(i);
\r
98 /** This is either equal to matchedFrom(i)+charsMatched(i) if the match
\r
99 was successful, or -1 if it was not. */
\r
100 public int matchedTo(int i) {
\r
101 if(marks==null||i>numSubs_||!didMatch_) return -1;
\r
102 return marks[i+numSubs_];
\r
104 /** Obtains a substring matching the nth set
\r
105 of parenthesis from the pattern. See
\r
106 numSubs(void), or null if the nth backrefence did
\r
108 public String stringMatched(int i) {
\r
109 int mf = matchedFrom(i), cm = charsMatched(i);
\r
110 return !didMatch_ || mf<0 || cm<0 ? null :
\r
111 src.substring(mf,mf+cm);
\r
113 /** This returns the part of the string that preceeds the match,
\r
114 or null if the match failed.*/
\r
115 public String left() {
\r
116 int mf = matchedFrom();
\r
117 return !didMatch_ || (mf<0) ? null : src.substring(0,mf);
\r
119 /** This returns the part of the string that follows the ith
\r
120 backreference, or null if the backreference did not match. */
\r
121 public String left(int i) {
\r
122 int mf = matchedFrom(i);
\r
123 return !didMatch_ || (mf<0) ? null : src.substring(0,mf);
\r
125 /** This returns the part of the string that follows the match,
\r
126 or null if the backreference did not match.*/
\r
127 public String right() {
\r
128 int mf = matchedFrom(), cm = charsMatched();
\r
129 return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf+
\r
132 /** This returns the string to the right of the ith backreference,
\r
133 or null if the backreference did not match. */
\r
134 public String right(int i) {
\r
135 int mf = matchedFrom(i), cm = charsMatched(i);
\r
136 return !didMatch_ || mf<0 || cm<0 ? null :
\r
137 src.substring(mf+cm,src.length());
\r
139 /** After a successful match, this returns the location of
\r
140 the first matching character, or -1 if the match failed.*/
\r
141 public int matchedFrom() { return !didMatch_ ? -1 : matchFrom_; }
\r
142 /** After a successful match, this returns the number of
\r
143 characters in the match, or -1 if the match failed. */
\r
144 public int charsMatched() { return !didMatch_||matchFrom_<0 ? -1 : charsMatched_; }
\r
145 /** This is matchedFrom()+charsMatched() after a successful match,
\r
146 or -1 otherwise. */
\r
147 public int matchedTo() { return !didMatch_ ? -1 : matchFrom_+charsMatched_;}
\r
148 /** This returns the number of
\r
149 backreferences (parenthesis) in the pattern,
\r
150 i.e. the pattern "(ab)" has
\r
151 one, the pattern "(a)(b)" has two, etc. */
\r
152 public int numSubs() { return numSubs_; }
\r
153 /** Contains true if the last match was successful. */
\r
154 public boolean didMatch() { return didMatch_; }
\r
156 /** An older name for matchedFrom. */
\r
157 public int matchFrom() { return matchedFrom(); }
\r
158 /** An older name for stringMatched(). */
\r
159 public String substring() { return stringMatched(); }
\r
160 /** An older name for matchedFrom. */
\r
161 public int matchFrom(int i) { return matchedFrom(i); }
\r
162 /** An older name for stringMatched. */
\r
163 public String substring(int i) { return stringMatched(i); }
\r