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;
10 import java.util.Enumeration;
11 import java.util.Vector;
14 Shareware: package pat
15 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
18 * The RegexTokenizer is similar to the StringTokenizer class provided with
19 * java, but allows one to tokenize using regular expressions, rather than a
20 * simple list of characters. Tokens are any strings between the supplied
21 * regular expression, as well as any backreferences (things in parenthesis)
22 * contained within the regular expression.
24 public class RegexTokenizer implements Enumeration
32 Vector v = new Vector();
34 Vector vi = new Vector();
43 if (r.searchFrom(toParse, pos))
45 v.addElement(r.left().substring(pos));
46 vi.addElement(Integer.valueOf(r.matchFrom() + r.charsMatched()));
47 for (int i = 0; i < r.numSubs(); i++)
49 if (r.substring() != null)
51 v.addElement(r.substring(i + offset));
52 vi.addElement(Integer.valueOf(r.matchFrom(i + offset)
53 + r.charsMatched(i + offset)));
56 pos = r.matchFrom() + r.charsMatched();
64 /** Initialize the tokenizer with a string of text and a pattern */
65 public RegexTokenizer(String txt, String ptrn)
69 offset = Regex.BackRefOffset;
73 /** Initialize the tokenizer with a Regex object. */
74 public RegexTokenizer(String txt, Regex r)
78 offset = Regex.BackRefOffset;
83 * This should always be cast to a String, as in StringTokenizer, and as in
84 * StringTokenizer one can do this by calling nextString().
86 public Object nextElement()
88 if (count >= v.size())
92 return v.elementAt(count++);
95 /** This is the equivalent (String)nextElement(). */
96 public String nextToken()
98 return (String) nextElement();
102 * This asks for the next token, and changes the pattern being used at the
105 public String nextToken(String newpat)
110 } catch (RegSyntax r_)
117 * This asks for the next token, and changes the pattern being used at the
120 public String nextToken(Regex nr)
123 if (vi.size() > count)
125 pos = ((Integer) vi.elementAt(count)).intValue();
133 /** Tells whether there are more tokens in the pattern. */
134 public boolean hasMoreElements()
136 if (count >= v.size())
140 return count < v.size();
144 * Tells whether there are more tokens in the pattern, but in the fashion of
147 public boolean hasMoreTokens()
149 return hasMoreElements();
152 /** Determines the # of remaining tokens */
153 public int countTokens()
156 while (hasMoreTokens())
161 return v.size() - count;
164 /** Returns all tokens in the String */
165 public String[] allTokens()
168 String[] ret = new String[v.size()];