public class RE extends REToken
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, a Reader or an InputStream of some sort. Note that when using a Reader or 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 a Reader or InputStream, this means an offset from the current read position, so subsequent calls with the same index argument on a Reader or an InputStream will not necessarily access 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).
All operations on a regular expression are performed in a thread-safe manner.
Modifier and Type | Field and Description |
---|---|
private REToken |
firstToken |
private REToken |
lastToken |
private static ResourceBundle |
messages |
private int |
minimumLength
Minimum length, in characters, of any possible match.
|
private int |
numSubs |
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_NO_INTERPOLATE
Execution flag.
|
static int |
REG_NOTBOL
Execution flag.
|
static int |
REG_NOTEOL
Execution flag.
|
private static String |
VERSION |
Modifier | Constructor and Description |
---|---|
protected |
RE() |
|
RE(Object pattern)
Constructs a regular expression pattern buffer without any compilation
flags set, and using the default syntax (RESyntax.RE_SYNTAX_PERL5).
|
|
RE(Object pattern,
int cflags)
Constructs a regular expression pattern buffer using the specified
compilation flags and the default syntax (RESyntax.RE_SYNTAX_PERL5).
|
|
RE(Object pattern,
int cflags,
RESyntax syntax)
Constructs a regular expression pattern buffer using the specified
compilation flags and regular expression syntax.
|
private |
RE(Object patternObj,
int cflags,
RESyntax syntax,
int myIndex,
int nextSub) |
private |
RE(REToken first,
REToken last,
int subs,
int subIndex,
int minLength) |
Modifier and Type | Method and Description |
---|---|
private void |
addToken(REToken next) |
(package private) boolean |
chain(REToken next) |
(package private) void |
dump(StringBuffer os) |
REMatch[] |
getAllMatches(Object input)
Returns an array of all matches found in the input.
|
REMatch[] |
getAllMatches(Object input,
int index)
Returns an array of all matches found in the input,
beginning at the specified index position.
|
REMatch[] |
getAllMatches(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(CharIndexed input,
int index,
int eflags) |
private static int |
getCharUnit(char[] input,
int index,
CharUnit unit) |
(package private) static String |
getLocalizedMessage(String key) |
REMatch |
getMatch(Object input)
Returns the first match found in the input.
|
REMatch |
getMatch(Object input,
int index)
Returns the first match found in the input, beginning
the search at the specified index.
|
REMatch |
getMatch(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(Object input,
int index,
int eflags,
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(Object input)
Returns an REMatchEnumeration that can be used to iterate over the
matches found in the input text.
|
REMatchEnumeration |
getMatchEnumeration(Object input,
int index)
Returns an REMatchEnumeration that can be used to iterate over the
matches found in the input text.
|
REMatchEnumeration |
getMatchEnumeration(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(CharIndexed input,
int anchor,
int eflags,
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,
StringBuffer buf) |
protected void |
initialize(Object patternObj,
int cflags,
RESyntax syntax,
int myIndex,
int nextSub) |
boolean |
isMatch(Object input)
Checks if the regular expression matches the input in its entirety.
|
boolean |
isMatch(Object input,
int index)
Checks if the input string, starting from index, is an exact match of
this regular expression.
|
boolean |
isMatch(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(CharIndexed input,
int index,
int eflags) |
private static CharIndexed |
makeCharIndexed(Object input,
int index) |
(package private) boolean |
match(CharIndexed input,
REMatch mymatch)
Returns true if the match succeeded, false if it failed.
|
private static REToken |
setRepeated(REToken current,
int min,
int max,
int index) |
(package private) void |
setUncle(REToken uncle) |
String |
substitute(Object input,
String replace)
Substitutes the replacement text for the first match found in the input.
|
String |
substitute(Object input,
String replace,
int index)
Substitutes the replacement text for the first match found in the input
beginning at the specified index position.
|
String |
substitute(Object input,
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.
|
String |
substituteAll(Object input,
String replace)
Substitutes the replacement text for each non-overlapping match found
in the input text.
|
String |
substituteAll(Object input,
String replace,
int index)
Substitutes the replacement text for each non-overlapping match found
in the input text, starting at the specified index.
|
String |
substituteAll(Object input,
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 String |
substituteAllImpl(CharIndexed input,
String replace,
int index,
int eflags) |
private String |
substituteImpl(CharIndexed input,
String replace,
int index,
int eflags) |
String |
toString()
Return a human readable form of the compiled regular expression,
useful for debugging.
|
static String |
version()
Returns a string representing the version of the gnu.regexp package.
|
private static final String VERSION
private static ResourceBundle messages
private REToken firstToken
private int numSubs
private int minimumLength
public static final int REG_ICASE
public static final int REG_DOT_NEWLINE
public static final int REG_MULTILINE
public static final int REG_NOTBOL
This example demonstrates the results of various ways of matching on a substring.
// Results:
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);
// m0 = "food"
// m1 = "fool"
// m2 = null
// m3 = null
// m4 = "fool"
public static final int REG_NOTEOL
public static final int REG_ANCHORINDEX
public static final int REG_NO_INTERPOLATE
public RE(Object pattern) throws REException
pattern
- A regular expression pattern, in the form of a String,
StringBuffer or char[]. Other input types will be converted to
strings using the toString() method.REException
- The input pattern could not be parsed.NullPointerException
- The pattern was null.public RE(Object pattern, int cflags) throws REException
pattern
- A regular expression pattern, in the form of a String,
StringBuffer, or char[]. Other input types will be converted to
strings using the toString() method.cflags
- The logical OR of any combination of the compilation flags listed above.REException
- The input pattern could not be parsed.NullPointerException
- The pattern was null.public RE(Object pattern, int cflags, RESyntax syntax) throws REException
pattern
- A regular expression pattern, in the form of a String,
StringBuffer, or char[]. Other input types will be converted to
strings using the toString() method.cflags
- The logical OR of any combination of the compilation flags listed above.syntax
- The type of regular expression syntax to use.REException
- The input pattern could not be parsed.NullPointerException
- The pattern was null.private RE(Object patternObj, int cflags, RESyntax syntax, int myIndex, int nextSub) throws REException
REException
protected RE()
public static final String version()
static final String getLocalizedMessage(String key)
protected void initialize(Object patternObj, int cflags, RESyntax syntax, int myIndex, int nextSub) throws REException
REException
private static int getCharUnit(char[] input, int index, CharUnit unit) throws REException
REException
public boolean isMatch(Object input)
input
- The input text.public boolean isMatch(Object input, int index)
input
- The input text.index
- The offset index at which the search should be begin.public boolean isMatch(Object input, int index, int eflags)
input
- The input text.index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.private boolean isMatchImpl(CharIndexed input, int index, int eflags)
public int getNumSubs()
public int getMinimumLength()
getMinimumLength
in class REToken
public REMatch[] getAllMatches(Object input)
input
- The input text.public REMatch[] getAllMatches(Object input, int index)
input
- The input text.index
- The offset index at which the search should be begin.public REMatch[] getAllMatches(Object input, int index, int eflags)
input
- The input text.index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.private REMatch[] getAllMatchesImpl(CharIndexed input, int index, int eflags)
boolean match(CharIndexed input, REMatch mymatch)
REToken
public REMatch getMatch(Object input)
input
- The input text.public REMatch getMatch(Object input, int index)
input
- The input text.index
- The offset within the text to begin looking for a match.public REMatch getMatch(Object input, int index, int eflags)
input
- The input text.index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.public REMatch getMatch(Object input, int index, int eflags, StringBuffer buffer)
input
- The input text.index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.buffer
- The StringBuffer to save pre-match text in.REMatch getMatchImpl(CharIndexed input, int anchor, int eflags, StringBuffer buffer)
public REMatchEnumeration getMatchEnumeration(Object input)
input
- The input text.public REMatchEnumeration getMatchEnumeration(Object input, int index)
input
- The input text.index
- The offset index at which the search should be begin.public REMatchEnumeration getMatchEnumeration(Object input, int index, int eflags)
input
- The input text.index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.public String substitute(Object input, String replace)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).REMatch.substituteInto(java.lang.String)
public String substitute(Object input, String replace, int index)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).index
- The offset index at which the search should be begin.REMatch.substituteInto(java.lang.String)
public String substitute(Object input, String replace, int index, int eflags)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.REMatch.substituteInto(java.lang.String)
private String substituteImpl(CharIndexed input, String replace, int index, int eflags)
public String substituteAll(Object input, String replace)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).REMatch.substituteInto(java.lang.String)
public String substituteAll(Object input, String replace, int index)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).index
- The offset index at which the search should be begin.REMatch.substituteInto(java.lang.String)
public String substituteAll(Object input, String replace, int index, int eflags)
input
- The input text.replace
- The replacement text, which may contain $x metacharacters (see REMatch.substituteInto).index
- The offset index at which the search should be begin.eflags
- The logical OR of any execution flags above.REMatch.substituteInto(java.lang.String)
private String substituteAllImpl(CharIndexed input, String replace, int index, int eflags)
private static REToken setRepeated(REToken current, int min, int max, int index) throws REException
REException
private static int getPosixSet(char[] pattern, int index, StringBuffer buf)
private int getMinMax(char[] input, int index, IntPair minMax, RESyntax syntax) throws REException
REException
public String toString()
void dump(StringBuffer os)
private static CharIndexed makeCharIndexed(Object input, int index)
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses