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;
13 Shareware: package pat
14 <a href="copyright.html">Copyright 2001, Steven R. Brandt</a>
17 * The RegexTokenizer is similar to the StringTokenizer class provided with
18 * java, but allows one to tokenize using regular expressions, rather than a
19 * simple list of characters. Tokens are any strings between the supplied
20 * regular expression, as well as any backreferences (things in parenthesis)
21 * contained within the regular expression.
23 public class RegexTokenizer implements Enumeration
31 Vector v = new Vector();
33 Vector vi = new Vector();
42 if (r.searchFrom(toParse, pos))
44 v.addElement(r.left().substring(pos));
45 vi.addElement(new Integer(r.matchFrom() + r.charsMatched()));
46 for (int i = 0; i < r.numSubs(); i++)
48 if (r.substring() != null)
50 v.addElement(r.substring(i + offset));
51 vi.addElement(new Integer(r.matchFrom(i + offset)
52 + r.charsMatched(i + offset)));
55 pos = r.matchFrom() + r.charsMatched();
63 /** Initialize the tokenizer with a string of text and a pattern */
64 public RegexTokenizer(String txt, String ptrn)
68 offset = Regex.BackRefOffset;
72 /** Initialize the tokenizer with a Regex object. */
73 public RegexTokenizer(String txt, Regex r)
77 offset = Regex.BackRefOffset;
82 * This should always be cast to a String, as in StringTokenizer, and as in
83 * StringTokenizer one can do this by calling nextString().
85 public Object nextElement()
87 if (count >= v.size())
91 return v.elementAt(count++);
94 /** This is the equivalent (String)nextElement(). */
95 public String nextToken()
97 return (String) nextElement();
101 * This asks for the next token, and changes the pattern being used at the
104 public String nextToken(String newpat)
109 } catch (RegSyntax r_)
116 * This asks for the next token, and changes the pattern being used at the
119 public String nextToken(Regex nr)
122 if (vi.size() > count)
124 pos = ((Integer) vi.elementAt(count)).intValue();
132 /** Tells whether there are more tokens in the pattern. */
133 public boolean hasMoreElements()
135 if (count >= v.size())
139 return count < v.size();
143 * Tells whether there are more tokens in the pattern, but in the fashion of
146 public boolean hasMoreTokens()
148 return hasMoreElements();
151 /** Determines the # of remaining tokens */
152 public int countTokens()
155 while (hasMoreTokens())
160 return v.size() - count;
163 /** Returns all tokens in the String */
164 public String[] allTokens()
167 String[] ret = new String[v.size()];