Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

com.memoire.re
Class RE  view RE download RE.java

java.lang.Object
  extended bycom.memoire.re.REToken
      extended bycom.memoire.re.RE

public class RE
extends REToken

RE provides the user interface for compiling and matching regular expressions.

A regular expression object (class RE) is compiled by constructing it from a String, StringBuffer or character array, with optional compilation flags (below) and an optional syntax specification (see RESyntax; if not specified, RESyntax.RE_SYNTAX_PERL5 is used).

Various methods attempt to match input text against a compiled regular expression. These methods are:

  • isMatch: returns true if the input text in its entirety matches the regular expression pattern.
  • getMatch: returns the first match found in the input text, or null if no match is found.
  • getAllMatches: returns an array of all non-overlapping matches found in the input text. If no matches are found, the array is zero-length.
  • substitute: substitute the first occurence of the pattern in the input text with a replacement string (which may include metacharacters $0-$9, see REMatch.substituteInto).
  • substituteAll: same as above, but repeat for each match before returning.
  • getMatchEnumeration: returns an REMatchEnumeration object that allows iteration over the matches (see REMatchEnumeration for some reasons why you may want to do this instead of using getAllMatches.

    These methods all have similar argument lists. The input can be a String, a character array, a StringBuffer or an InputStream of some sort. Note that when using an InputStream, the stream read position cannot be guaranteed after attempting a match (this is not a bug, but a consequence of the way regular expressions work). Using an REMatchEnumeration can eliminate most positioning problems.

    The optional index argument specifies the offset from the beginning of the text at which the search should start (see the descriptions of some of the execution flags for how this can affect positional pattern operators). For an InputStream, this means an offset from the current read position, so subsequent calls with the same index argument on an InputStream will not necessarily be accessing the same position on the stream, whereas repeated searches at a given index in a fixed string will return consistent results.

    You can optionally affect the execution environment by using a combination of execution flags (constants listed below).

    Version:
    1.0.8, 21 March 1999

    Field Summary
    private  REToken firstToken
               
    private  REToken lastToken
               
    protected  REToken m_next
               
    private  int m_numSubs
               
    protected  int m_subIndex
               
    protected  REToken m_uncle
               
    (package private) static java.lang.String newline
               
    static int REG_ANCHORINDEX
              Execution flag.
    static int REG_DOT_NEWLINE
              Compilation flag.
    static int REG_ICASE
              Compilation flag.
    static int REG_MULTILINE
              Compilation flag.
    static int REG_NOTBOL
              Execution flag.
    static int REG_NOTEOL
              Execution flag.
    private static java.lang.String s_version
               
     
    Constructor Summary
      RE(java.lang.Object pattern)
              Constructs a regular expression pattern buffer without any compilation flags set, and using the default syntax (RESyntax.RE_SYNTAX_PERL5).
      RE(java.lang.Object pattern, int cflags)
              Constructs a regular expression pattern buffer using the specified compilation flags and the default syntax (RESyntax.RE_SYNTAX_PERL5).
      RE(java.lang.Object pattern, int cflags, RESyntax syntax)
              Constructs a regular expression pattern buffer using the specified compilation flags and regular expression syntax.
    private RE(java.lang.Object patternObj, int cflags, RESyntax syntax, int myIndex, int nextSub)
               
    private RE(REToken f_first, REToken f_last, int f_subs, int f_subIndex)
               
     
    Method Summary
    private  void addToken(REToken next)
               
    (package private)  boolean chain(REToken f_next)
               
    (package private)  void dump(java.lang.StringBuffer os)
               
    (package private)  void dumpAll(java.lang.StringBuffer os)
               
     REMatch[] getAllMatches(java.lang.Object input)
              Returns an array of all matches found in the input.
     REMatch[] getAllMatches(java.lang.Object input, int index)
              Returns an array of all matches found in the input, beginning at the specified index position.
     REMatch[] getAllMatches(java.lang.Object input, int index, int eflags)
              Returns an array of all matches found in the input string, beginning at the specified index position and using the specified execution flags.
    private  REMatch[] getAllMatchesImpl(RECharIndexed input, int index, int eflags)
               
    private static int getCharUnit(char[] input, int index, CharUnit unit)
               
     REMatch getMatch(java.lang.Object input)
              Returns the first match found in the input.
     REMatch getMatch(java.lang.Object input, int index)
              Returns the first match found in the input, beginning the search at the specified index.
     REMatch getMatch(java.lang.Object input, int index, int eflags)
              Returns the first match found in the input, beginning the search at the specified index, and using the specified execution flags.
     REMatch getMatch(java.lang.Object input, int index, int eflags, java.lang.StringBuffer buffer)
              Returns the first match found in the input, beginning the search at the specified index, and using the specified execution flags.
     REMatchEnumeration getMatchEnumeration(java.lang.Object input)
              Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.
     REMatchEnumeration getMatchEnumeration(java.lang.Object input, int index)
              Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.
     REMatchEnumeration getMatchEnumeration(java.lang.Object input, int index, int eflags)
              Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.
    (package private)  REMatch getMatchImpl(RECharIndexed input, int index, int eflags, java.lang.StringBuffer buffer)
               
     int getMinimumLength()
              Returns the minimum number of characters that could possibly constitute a match of this regular expression.
    private  int getMinMax(char[] input, int index, IntPair minMax, RESyntax syntax)
               
     int getNumSubs()
              Returns the maximum number of subexpressions in this regular expression.
    private static int getPosixSet(char[] pattern, int index, java.lang.StringBuffer buf)
               
     boolean isMatch(java.lang.Object input)
              Checks if the input in its entirety is an exact match of this regular expression.
     boolean isMatch(java.lang.Object input, int index)
              Checks if the input string, starting from index, is an exact match of this regular expression.
     boolean isMatch(java.lang.Object input, int index, int eflags)
              Checks if the input, starting from index and using the specified execution flags, is an exact match of this regular expression.
    private  boolean isMatchImpl(RECharIndexed input, int index, int eflags)
               
    private static RECharIndexed makeRECharIndexed(java.lang.Object input, int index)
               
    (package private)  int[] match(RECharIndexed input, int index, int eflags, REMatch mymatch)
               
    protected  int[] next(RECharIndexed input, int index, int eflags, REMatch mymatch)
               
    private static REToken setRepeated(REToken current, int min, int max, int index)
               
    (package private)  void setUncle(REToken f_uncle)
               
     java.lang.String substitute(java.lang.Object input, java.lang.String replace)
              Substitutes the replacement text for the first match found in the input.
     java.lang.String substitute(java.lang.Object input, java.lang.String replace, int index)
              Substitutes the replacement text for the first match found in the input beginning at the specified index position.
     java.lang.String substitute(java.lang.Object input, java.lang.String replace, int index, int eflags)
              Substitutes the replacement text for the first match found in the input string, beginning at the specified index position and using the specified execution flags.
     java.lang.String substituteAll(java.lang.Object input, java.lang.String replace)
              Substitutes the replacement text for each non-overlapping match found in the input text.
     java.lang.String substituteAll(java.lang.Object input, java.lang.String replace, int index)
              Substitutes the replacement text for each non-overlapping match found in the input text, starting at the specified index.
     java.lang.String substituteAll(java.lang.Object input, java.lang.String replace, int index, int eflags)
              Substitutes the replacement text for each non-overlapping match found in the input text, starting at the specified index and using the specified execution flags.
    private  java.lang.String substituteAllImpl(RECharIndexed input, java.lang.String replace, int index, int eflags)
               
    private  java.lang.String substituteImpl(RECharIndexed input, java.lang.String replace, int index, int eflags)
               
     java.lang.String toString()
              Return a human readable form of the compiled regular expression, useful for debugging.
    static java.lang.String version()
              Returns a string representing the version of the gnu.regexp package.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
     

    Field Detail

    s_version

    private static final java.lang.String s_version
    See Also:
    Constant Field Values

    firstToken

    private REToken firstToken

    lastToken

    private REToken lastToken

    m_numSubs

    private int m_numSubs

    REG_ICASE

    public static final int REG_ICASE
    Compilation flag. Do not differentiate case. Subsequent searches using this RE will be case insensitive.

    See Also:
    Constant Field Values

    REG_DOT_NEWLINE

    public static final int REG_DOT_NEWLINE
    Compilation flag. The match-any-character operator (dot) will match a newline character. When set this overrides the syntax bit RE_DOT_NEWLINE (see RESyntax for details). This is equivalent to the "/s" operator in Perl.

    See Also:
    Constant Field Values

    REG_MULTILINE

    public static final int REG_MULTILINE
    Compilation flag. Use multiline mode. In this mode, the ^ and $ anchors will match based on newlines within the input. This is equivalent to the "/m" operator in Perl.

    See Also:
    Constant Field Values

    REG_NOTBOL

    public static final int REG_NOTBOL
    Execution flag. The match-beginning operator (^) will not match at the beginning of the input string. Useful for matching on a substring when you know the context of the input is such that position zero of the input to the match test is not actually position zero of the text.

    This example demonstrates the results of various ways of matching on a substring.

    String s = "food bar fool";
    RE exp = new RE("^foo.");
    REMatch m0 = exp.getMatch(s);
    REMatch m1 = exp.getMatch(s.substring(8));
    REMatch m2 = exp.getMatch(s.substring(8),0,RE.REG_NOTBOL);
    REMatch m3 = exp.getMatch(s,8);
    REMatch m4 = exp.getMatch(s,8,RE.REG_ANCHORINDEX);

    // Results:
    // m0 = "food"
    // m1 = "fool"
    // m2 = null
    // m3 = null
    // m4 = "fool"

    See Also:
    Constant Field Values

    REG_NOTEOL

    public static final int REG_NOTEOL
    Execution flag. The match-end operator ($) does not match at the end of the input string. Useful for matching on substrings.

    See Also:
    Constant Field Values

    REG_ANCHORINDEX

    public static final int REG_ANCHORINDEX
    Execution flag. The match-beginning operator (^) matches not at position 0 in the input string, but at the position the search started at (based on the index input given to the getMatch function). See the example under REG_NOTBOL.

    See Also:
    Constant Field Values

    newline

    static final java.lang.String newline

    m_next

    protected REToken m_next

    m_uncle

    protected REToken m_uncle

    m_subIndex

    protected int m_subIndex
    Constructor Detail

    RE

    public RE(java.lang.Object pattern)
       throws REException
    Constructs a regular expression pattern buffer without any compilation flags set, and using the default syntax (RESyntax.RE_SYNTAX_PERL5).


    RE

    public RE(java.lang.Object pattern,
              int cflags)
       throws REException
    Constructs a regular expression pattern buffer using the specified compilation flags and the default syntax (RESyntax.RE_SYNTAX_PERL5).


    RE

    public RE(java.lang.Object pattern,
              int cflags,
              RESyntax syntax)
       throws REException
    Constructs a regular expression pattern buffer using the specified compilation flags and regular expression syntax.


    RE

    private RE(REToken f_first,
               REToken f_last,
               int f_subs,
               int f_subIndex)

    RE

    private RE(java.lang.Object patternObj,
               int cflags,
               RESyntax syntax,
               int myIndex,
               int nextSub)
        throws REException
    Method Detail

    version

    public static final java.lang.String version()
    Returns a string representing the version of the gnu.regexp package.


    getCharUnit

    private static int getCharUnit(char[] input,
                                   int index,
                                   CharUnit unit)
                            throws REException

    isMatch

    public boolean isMatch(java.lang.Object input)
    Checks if the input in its entirety is an exact match of this regular expression.


    isMatch

    public boolean isMatch(java.lang.Object input,
                           int index)
    Checks if the input string, starting from index, is an exact match of this regular expression.


    isMatch

    public boolean isMatch(java.lang.Object input,
                           int index,
                           int eflags)
    Checks if the input, starting from index and using the specified execution flags, is an exact match of this regular expression.


    isMatchImpl

    private boolean isMatchImpl(RECharIndexed input,
                                int index,
                                int eflags)

    getNumSubs

    public int getNumSubs()
    Returns the maximum number of subexpressions in this regular expression. If the expression contains branches, the value returned will be the maximum subexpressions in any of the branches.


    setUncle

    void setUncle(REToken f_uncle)
    Overrides:
    setUncle in class REToken

    chain

    boolean chain(REToken f_next)
    Overrides:
    chain in class REToken

    getMinimumLength

    public int getMinimumLength()
    Returns the minimum number of characters that could possibly constitute a match of this regular expression.

    Overrides:
    getMinimumLength in class REToken

    getAllMatches

    public REMatch[] getAllMatches(java.lang.Object input)
    Returns an array of all matches found in the input.


    getAllMatches

    public REMatch[] getAllMatches(java.lang.Object input,
                                   int index)
    Returns an array of all matches found in the input, beginning at the specified index position.


    getAllMatches

    public REMatch[] getAllMatches(java.lang.Object input,
                                   int index,
                                   int eflags)
    Returns an array of all matches found in the input string, beginning at the specified index position and using the specified execution flags.


    getAllMatchesImpl

    private REMatch[] getAllMatchesImpl(RECharIndexed input,
                                        int index,
                                        int eflags)

    match

    int[] match(RECharIndexed input,
                int index,
                int eflags,
                REMatch mymatch)
    Specified by:
    match in class REToken

    getMatch

    public REMatch getMatch(java.lang.Object input)
    Returns the first match found in the input.


    getMatch

    public REMatch getMatch(java.lang.Object input,
                            int index)
    Returns the first match found in the input, beginning the search at the specified index.


    getMatch

    public REMatch getMatch(java.lang.Object input,
                            int index,
                            int eflags)
    Returns the first match found in the input, beginning the search at the specified index, and using the specified execution flags. If no match is found, returns null.


    getMatch

    public REMatch getMatch(java.lang.Object input,
                            int index,
                            int eflags,
                            java.lang.StringBuffer buffer)
    Returns the first match found in the input, beginning the search at the specified index, and using the specified execution flags. If no match is found, returns null. If a StringBuffer is provided and is non-null, the contents of the input text from the index to the beginning of the match (or to the end of the input, if there is no match) are appended to the StringBuffer.


    getMatchImpl

    REMatch getMatchImpl(RECharIndexed input,
                         int index,
                         int eflags,
                         java.lang.StringBuffer buffer)

    getMatchEnumeration

    public REMatchEnumeration getMatchEnumeration(java.lang.Object input)
    Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.


    getMatchEnumeration

    public REMatchEnumeration getMatchEnumeration(java.lang.Object input,
                                                  int index)
    Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.


    getMatchEnumeration

    public REMatchEnumeration getMatchEnumeration(java.lang.Object input,
                                                  int index,
                                                  int eflags)
    Returns an REMatchEnumeration that can be used to iterate over the matches found in the input text.


    substitute

    public java.lang.String substitute(java.lang.Object input,
                                       java.lang.String replace)
    Substitutes the replacement text for the first match found in the input.


    substitute

    public java.lang.String substitute(java.lang.Object input,
                                       java.lang.String replace,
                                       int index)
    Substitutes the replacement text for the first match found in the input beginning at the specified index position.


    substitute

    public java.lang.String substitute(java.lang.Object input,
                                       java.lang.String replace,
                                       int index,
                                       int eflags)
    Substitutes the replacement text for the first match found in the input string, beginning at the specified index position and using the specified execution flags.


    substituteImpl

    private java.lang.String substituteImpl(RECharIndexed input,
                                            java.lang.String replace,
                                            int index,
                                            int eflags)

    substituteAll

    public java.lang.String substituteAll(java.lang.Object input,
                                          java.lang.String replace)
    Substitutes the replacement text for each non-overlapping match found in the input text.


    substituteAll

    public java.lang.String substituteAll(java.lang.Object input,
                                          java.lang.String replace,
                                          int index)
    Substitutes the replacement text for each non-overlapping match found in the input text, starting at the specified index.


    substituteAll

    public java.lang.String substituteAll(java.lang.Object input,
                                          java.lang.String replace,
                                          int index,
                                          int eflags)
    Substitutes the replacement text for each non-overlapping match found in the input text, starting at the specified index and using the specified execution flags.


    substituteAllImpl

    private java.lang.String substituteAllImpl(RECharIndexed input,
                                               java.lang.String replace,
                                               int index,
                                               int eflags)

    addToken

    private void addToken(REToken next)

    setRepeated

    private static REToken setRepeated(REToken current,
                                       int min,
                                       int max,
                                       int index)
                                throws REException

    getPosixSet

    private static int getPosixSet(char[] pattern,
                                   int index,
                                   java.lang.StringBuffer buf)

    getMinMax

    private int getMinMax(char[] input,
                          int index,
                          IntPair minMax,
                          RESyntax syntax)
                   throws REException

    toString

    public java.lang.String toString()
    Return a human readable form of the compiled regular expression, useful for debugging.


    dump

    void dump(java.lang.StringBuffer os)
    Overrides:
    dump in class REToken

    makeRECharIndexed

    private static RECharIndexed makeRECharIndexed(java.lang.Object input,
                                                   int index)

    next

    protected int[] next(RECharIndexed input,
                         int index,
                         int eflags,
                         REMatch mymatch)

    dumpAll

    void dumpAll(java.lang.StringBuffer os)