kmy.regex.util
Class Regex

java.lang.Object
  |
  +--kmy.regex.util.Regex
Direct Known Subclasses:
InterpretedRegex, JavaClassRegexStub, ProxyRegex

public abstract class Regex
extends java.lang.Object
implements java.lang.Cloneable, java.io.FilenameFilter

Regular expression interface definition and convenience methods. Interface focuses on efficiency and convenience methods can be used when simplicity is more important (most of the times).

Regex can be used either for search or for matching. Matching determines if string maches regex exactly and search determines if there is a substring of the given string that matches the regex. There is matchWhole and match methods; matchWhole is "pure" match, it tries to match the whole string; match tries to match beginning of the string.

Regex can contain embedded variables of two types. External variables are referenced with dollar sign ($), but not 'at' sign (@). They must be assigned prior to search or matching. Internal variables are either implicit (1, 2, etc) - those refer to unnamed subexpressions in brackets (for example regex (.)(.)=\1\2 contains two implicit variables: 1 and 2), or explicit - those are given names (with '@' constructions). Internal variables may be assigned values in the process of matching and their values can be obtained from the regex that was used for search or matching.

Regex contains some "working space" for the matching process. Therefore between the match/search call and till all information about match/search is obtained regex should be used only by a single thread. Use clone() or cloneRegex() to get a separate copy of it.

All 'end' or 'final' substring positions or indices in the buffer refer to the first character after substring.


Field Summary
private static RegexFactory factory
           
 
Constructor Summary
Regex()
           
 
Method Summary
 boolean accept(java.io.File dir, java.lang.String name)
           
 java.lang.Object clone()
           
 Regex cloneRegex()
           
static Regex createFilePattern(java.lang.String re)
           
protected static Regex createLowerCaseRegex(java.lang.String re)
           
static Regex createRegex(char[] arr, int off, int len)
           
static Regex createRegex(java.lang.String re)
           
static Regex createRegex(java.lang.String re, boolean ignoreCase)
           
 java.lang.String get(java.lang.String var)
           
abstract  char[] getCharBuffer(int extHandle)
          Returns external variable buffer by its buffer handle.
abstract  int getExtVariableHandle(java.lang.String var)
          Returns external variable buffer handle that can be used later to get or set variable value efficiently.
abstract  int getIndex(int handle)
          Returns external or internal variable index using its index handle.
 CharString getMatch()
           
abstract  int getMatchEnd()
          After successiful search returns matching substring's final position in the input buffer (that was set up by init method).
abstract  int getMatchStart()
          After successiful search returns matching substring's initial position in the input buffer (that was set up by init method).
 java.lang.String getMatchString()
           
abstract  int getVariableHandle(java.lang.String var, boolean begin)
          Returns variable index handle that can be used later to get or set variable value efficiently.
 void init(char[] arr)
           
abstract  void init(char[] arr, int off, int len)
          Prepares to match or search.
 void init(java.lang.String s)
           
private static void initFactory()
           
abstract  boolean match()
          Match the beginning of the buffer (that was set up by init method) against this regex.
 boolean matches(char[] arr, int off, int len)
           
 boolean matches(java.lang.String s)
           
 boolean matchesWhole(char[] arr, int off, int len)
           
 boolean matchesWhole(java.lang.String s)
           
abstract  boolean matchWhole()
          Match the buffer (that was set up by init method) against this regex.
abstract  boolean search()
          Search the buffer (that was set up by init method) for a substring that matches this regex.
abstract  boolean searchAgain()
          Search the buffer (that was set up by init method) for a substring that matches this regex.
 boolean searchOnce(char[] arr, int off, int len)
           
 boolean searchOnce(CharString s)
           
 boolean searchOnce(java.lang.Object obj)
           
 boolean searchOnce(java.lang.String s)
           
 void set(java.lang.String var, java.lang.String val)
           
 void setExtVariable(int extHandle, int beginHandle, int endHandle, char[] carr)
           
 void setExtVariable(int extHandle, int beginHandle, int endHandle, CharString v)
           
 void setExtVariable(int extHandle, int beginHandle, int endHandle, java.lang.Object v)
           
 void setExtVariable(int extHandle, int beginHandle, int endHandle, java.lang.String v)
           
abstract  void setExtVariableBuffer(int extHandle, char[] arr)
          Assignes external variable buffer by its buffer handle.
static void setFactory(RegexFactory _factory)
           
abstract  void setIndex(int handle, int value)
          Returns internal variable index using its index handle.
abstract  void setRefilledBuffer(char[] buffer)
          Refiller can call this method after refilling the buffer, if buffer has been reallocated.
abstract  void setRefiller(RegexRefiller refiller)
           
abstract  java.util.Enumeration variables()
          Enumerates all external and internal variables in this regex.
 
Methods inherited from class java.lang.Object
, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

factory

private static RegexFactory factory
Constructor Detail

Regex

public Regex()
Method Detail

getVariableHandle

public abstract int getVariableHandle(java.lang.String var,
                                      boolean begin)
Returns variable index handle that can be used later to get or set variable value efficiently. If efficiency is not a concern, use get and set methods instead. Every variable has two index handles - 'begin' and 'end' index handle.

getExtVariableHandle

public abstract int getExtVariableHandle(java.lang.String var)
Returns external variable buffer handle that can be used later to get or set variable value efficiently. If efficiency is not a concern, use get and set methods instead. Only external variables have buffer handle. This is different from index handles. For internal variables this method returs -1.

variables

public abstract java.util.Enumeration variables()
Enumerates all external and internal variables in this regex.

init

public abstract void init(char[] arr,
                          int off,
                          int len)
Prepares to match or search. Sets up an input buffer and a range of characters in it that is to be searched or matched.

searchAgain

public abstract boolean searchAgain()
Search the buffer (that was set up by init method) for a substring that matches this regex. See description of this class for difference between search and match. On success do not discard record of all other possible matches, so if this method is called again, different way of matching can be found for already found substring or a substring overlapping already found substring. Most often, search(), not searchAgain() should be used.

search

public abstract boolean search()
Search the buffer (that was set up by init method) for a substring that matches this regex. See description of this class for difference between search and match. All records for other possible matches withing matching substring that was found are destroyed, so if this method is called again, search will start at the first position after previously found substring.

matchWhole

public abstract boolean matchWhole()
Match the buffer (that was set up by init method) against this regex. See description of this class for difference between search/match/matchWhole.

match

public abstract boolean match()
Match the beginning of the buffer (that was set up by init method) against this regex. See description of this class for difference between search/match/matchWhole.

getIndex

public abstract int getIndex(int handle)
Returns external or internal variable index using its index handle. Every variable has two indices - 'begin' and 'end'. Use 'begin' and 'end' handles to get them. These indices can be used to determine which part of the input buffer (set by init method) corresponding internal variable matched. An external variable 'begin' and 'end' indices point to its own buffer. They are assinged by setIndex and are never changed.

setIndex

public abstract void setIndex(int handle,
                              int value)
Returns internal variable index using its index handle. Every variable has two indices - 'begin' and 'end'. Use 'begin' and 'end' handles to assign them. These indices are needed to completely specify value of an external variable.

getCharBuffer

public abstract char[] getCharBuffer(int extHandle)
Returns external variable buffer by its buffer handle. If -1 is passed as a handle the input buffer that was set up for matching (using init) is returned.

getMatchStart

public abstract int getMatchStart()
After successiful search returns matching substring's initial position in the input buffer (that was set up by init method).

getMatchEnd

public abstract int getMatchEnd()
After successiful search returns matching substring's final position in the input buffer (that was set up by init method).

setExtVariableBuffer

public abstract void setExtVariableBuffer(int extHandle,
                                          char[] arr)
Assignes external variable buffer by its buffer handle. It is needed to completely specify value of an external variable.

setRefiller

public abstract void setRefiller(RegexRefiller refiller)

setRefilledBuffer

public abstract void setRefilledBuffer(char[] buffer)
Refiller can call this method after refilling the buffer, if buffer has been reallocated.

matches

public boolean matches(char[] arr,
                       int off,
                       int len)

matches

public boolean matches(java.lang.String s)

matchesWhole

public boolean matchesWhole(char[] arr,
                            int off,
                            int len)

matchesWhole

public boolean matchesWhole(java.lang.String s)

searchOnce

public boolean searchOnce(char[] arr,
                          int off,
                          int len)

searchOnce

public boolean searchOnce(java.lang.String s)

searchOnce

public boolean searchOnce(CharString s)

searchOnce

public boolean searchOnce(java.lang.Object obj)

setExtVariable

public void setExtVariable(int extHandle,
                           int beginHandle,
                           int endHandle,
                           java.lang.String v)

setExtVariable

public void setExtVariable(int extHandle,
                           int beginHandle,
                           int endHandle,
                           CharString v)

setExtVariable

public void setExtVariable(int extHandle,
                           int beginHandle,
                           int endHandle,
                           char[] carr)

setExtVariable

public void setExtVariable(int extHandle,
                           int beginHandle,
                           int endHandle,
                           java.lang.Object v)

getMatch

public CharString getMatch()

getMatchString

public java.lang.String getMatchString()

get

public java.lang.String get(java.lang.String var)

set

public void set(java.lang.String var,
                java.lang.String val)

cloneRegex

public Regex cloneRegex()

init

public void init(char[] arr)

init

public void init(java.lang.String s)

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

createRegex

public static Regex createRegex(java.lang.String re)

createRegex

public static Regex createRegex(java.lang.String re,
                                boolean ignoreCase)

createFilePattern

public static Regex createFilePattern(java.lang.String re)

createRegex

public static Regex createRegex(char[] arr,
                                int off,
                                int len)

createLowerCaseRegex

protected static Regex createLowerCaseRegex(java.lang.String re)

setFactory

public static void setFactory(RegexFactory _factory)

initFactory

private static void initFactory()

accept

public boolean accept(java.io.File dir,
                      java.lang.String name)
Specified by:
accept in interface java.io.FilenameFilter