// // This software is now distributed according to // the Lesser Gnu Public License. Please see // http://www.gnu.org/copyleft/lesser.txt for // the details. // -- Happy Computing! // package com.stevesoft.pat; import java.util.*; /** Shareware: package pat Copyright 2001, Steven R. Brandt */ /** This class is used to store a result from Regex */ public class RegRes implements Cloneable { protected int[] marks = null; protected boolean didMatch_ = false; protected StringLike src=null; /** Obtain the text String that was matched against. */ public String getString() { return src.toString(); } /** Obtain the source StringLike object. */ public StringLike getStringLike() { return src; } protected int charsMatched_=0,matchFrom_=0,numSubs_=0; public String toString() { StringBuffer sb = new StringBuffer(); sb.append("match="+matchedFrom()+":"+charsMatched()); if(!didMatch()) return sb.toString(); for(int i=0;inumSubs_) return -1; //Integer in=(Integer)marks.get("left"+i); //return in == null ? -1 : in.intValue(); return marks[i]; } /** Obtains the number of characters matched by backreference i, or -1 if backreference i was not matched. */ public int charsMatched(int i) { if(marks==null||i>numSubs_||!didMatch_) return -1; //Integer in = (Integer)marks.get("right"+i); //int i2 = in==null ? -1 : in.intValue(); int mf = matchedFrom(i); return mf < 0 ? -1 : marks[i+numSubs_]-matchedFrom(i); } /** This is either equal to matchedFrom(i)+charsMatched(i) if the match was successful, or -1 if it was not. */ public int matchedTo(int i) { if(marks==null||i>numSubs_||!didMatch_) return -1; return marks[i+numSubs_]; } /** Obtains a substring matching the nth set of parenthesis from the pattern. See numSubs(void), or null if the nth backrefence did not match. */ public String stringMatched(int i) { int mf = matchedFrom(i), cm = charsMatched(i); return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf,mf+cm); } /** This returns the part of the string that preceeds the match, or null if the match failed.*/ public String left() { int mf = matchedFrom(); return !didMatch_ || (mf<0) ? null : src.substring(0,mf); } /** This returns the part of the string that follows the ith backreference, or null if the backreference did not match. */ public String left(int i) { int mf = matchedFrom(i); return !didMatch_ || (mf<0) ? null : src.substring(0,mf); } /** This returns the part of the string that follows the match, or null if the backreference did not match.*/ public String right() { int mf = matchedFrom(), cm = charsMatched(); return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf+ cm,src.length()); } /** This returns the string to the right of the ith backreference, or null if the backreference did not match. */ public String right(int i) { int mf = matchedFrom(i), cm = charsMatched(i); return !didMatch_ || mf<0 || cm<0 ? null : src.substring(mf+cm,src.length()); } /** After a successful match, this returns the location of the first matching character, or -1 if the match failed.*/ public int matchedFrom() { return !didMatch_ ? -1 : matchFrom_; } /** After a successful match, this returns the number of characters in the match, or -1 if the match failed. */ public int charsMatched() { return !didMatch_||matchFrom_<0 ? -1 : charsMatched_; } /** This is matchedFrom()+charsMatched() after a successful match, or -1 otherwise. */ public int matchedTo() { return !didMatch_ ? -1 : matchFrom_+charsMatched_;} /** This returns the number of backreferences (parenthesis) in the pattern, i.e. the pattern "(ab)" has one, the pattern "(a)(b)" has two, etc. */ public int numSubs() { return numSubs_; } /** Contains true if the last match was successful. */ public boolean didMatch() { return didMatch_; } /** An older name for matchedFrom. */ public int matchFrom() { return matchedFrom(); } /** An older name for stringMatched(). */ public String substring() { return stringMatched(); } /** An older name for matchedFrom. */ public int matchFrom(int i) { return matchedFrom(i); } /** An older name for stringMatched. */ public String substring(int i) { return stringMatched(i); } }