public class Regexp
extends java.lang.Object
convenience class for matching regular expressions.
Important Note: If you find yourself usingRegexp
objects in a loop, exercising a lengthy input, you are on the wrong track. Consider using aDfaRun
to do the looping for you and concentrate on the work to be done for each match.
When you don't need high speed filtering of large amounts of
input data, using Nfa/Dfa/FaAction/DfaRun
is a bit
tedious for just checking whether a string matches a
regular expression. This class provides a simpler interface.
Note that this implementation is not synchronized. Objects of this class should never be shared between threads without explicit synchronization by other means.
Constructor and Description |
---|
Regexp(java.lang.CharSequence re)
create a
Regexp object for the given regular
expression. |
Modifier and Type | Method and Description |
---|---|
int |
atStartOf(java.lang.CharSequence s)
tests whether
this matches a prefix of
s . |
static int |
atStartOf(java.lang.CharSequence regexp,
java.lang.CharSequence s,
int start)
tests whether
regexp matches within
s at position start . |
int |
atStartOf(java.lang.CharSequence s,
int pos)
tests whether
this matches a prefix of
s starting at position pos . |
int |
find(java.lang.CharSequence s)
tries to find
this in s . |
static int |
find(java.lang.CharSequence regexp,
java.lang.CharSequence s,
int start)
tries to find
regexp in s starting at
position start . |
int |
find(java.lang.CharSequence s,
int start)
tries to find
this in s starting at
position start . |
Dfa |
getDfa() |
int |
length()
returns the number of characters matched by the most recent
match call to any of
matches ,
atStartOf or find . |
boolean |
matches(java.lang.CharSequence s)
checks if the whole input sequence can be matched.
|
static boolean |
matches(java.lang.CharSequence regexp,
java.lang.CharSequence s,
int start)
checks if the complete tail of
s starting at
start matches regexp . |
boolean |
matches(java.lang.CharSequence s,
int pos)
checks if the whole input sequence starting at position
pos can be matched. |
TextStore |
submatches()
returns a
TextStore which contains submatches, if any,
pertaining to the most recent match. |
public Regexp(java.lang.CharSequence re)
create a Regexp
object for the given regular
expression.
java.lang.IllegalArgumentException
- in case
re
contains syntax errors.public Dfa getDfa()
public boolean matches(java.lang.CharSequence s, int pos)
checks if the whole input sequence starting at position
pos
can be matched.
true
iff the whole input sequence starting
at position pos
and ending at
s.length()
can be matched.public boolean matches(java.lang.CharSequence s)
checks if the whole input sequence can be matched. This method
is equivalent to a call to matches(s, 0)
.
public static boolean matches(java.lang.CharSequence regexp, java.lang.CharSequence s, int start)
checks if the complete tail of s
starting at
start
matches regexp
.
For anything but
casual uses consider to create a Regexp
object and
reuse it.
public int find(java.lang.CharSequence s, int start)
tries to find this
in s
starting at
position start
.
s
,
i.e. not relative to start
.
If not match can be found, -1 is returned.public int find(java.lang.CharSequence s)
tries to find this
in s
. This method
is equivalent to a call to find(s, 0)
.
public static int find(java.lang.CharSequence regexp, java.lang.CharSequence s, int start)
tries to find regexp
in s
starting at
position start
.
For anything but casual uses consider to
create a Regexp
object and reuse it.
s
,
i.e. not relative to start
.
If no match can be found, -1 is returned.public int atStartOf(java.lang.CharSequence s, int pos)
tests whether this
matches a prefix of
s
starting at position pos
.
public int atStartOf(java.lang.CharSequence s)
tests whether this
matches a prefix of
s
. This method
is equivalent to a call to atStartOf(s, 0)
.
public static int atStartOf(java.lang.CharSequence regexp, java.lang.CharSequence s, int start)
tests whether regexp
matches within
s
at position start
.
For anything but
casual uses consider to create a Regexp
object and
reuse it.
public int length()
returns the number of characters matched by the most recent
match call to any of matches
,
atStartOf
or find
.
java.lang.IllegalStateException
- if the most recent application of
this
did not yield a match.public TextStore submatches()
returns a TextStore
which contains submatches, if any,
pertaining to the most recent match. The object returned should
be treated read-only. Its contents are only valid until the next
match operation.
java.lang.IllegalStateException
- if the most recent application of
this
did not yield a match.