public class StringMatcher
extends java.lang.Object
Substrings matching patterns are discoverd with the findNext(DfaState)
and
matchAt(DfaState, int)
methods, both of which take a DFA start state for the
patterns to find.
NOTE that you don't have to pass the same state every time -- different calls with the same matcher can search for different patterns and return different kinds of results.
3 pointers are maintained in the string:
Constructor and Description |
---|
StringMatcher(java.lang.String src)
Create a new StringMatcher.
|
Modifier and Type | Method and Description |
---|---|
<MATCHRESULT> |
findNext(DfaState<MATCHRESULT> state)
Find the next non-empty match
|
java.lang.String |
getLastMatch()
Get the last successful matching substring, or "" if there isn't one.
|
int |
getLastMatchEnd()
Get the end position of the last successful match, or 0 if there isn't one
|
int |
getLastMatchStart()
Get the start position of the last successful match, or 0 if there isn't one
|
<MATCHRESULT> |
matchAt(DfaState<MATCHRESULT> state,
int startPos)
Find the longest match starting at a given position.
|
static <MATCHRESULT> |
matchWholeString(DfaState<MATCHRESULT> state,
java.lang.String str)
See if a whole string matches a DFA
|
void |
reset()
Resets the matcher to its initial state
|
void |
setPositions(int lastMatchStart,
int lastMatchEnd,
int searchLimit)
Set the LastMatchStart, LastMatchEnd, and SearchLimit positions explicitly.
|
public StringMatcher(java.lang.String src)
The LastMatchStart and LastMatchEnd positions are initialized to zero
src
- the source string to be searchedpublic void setPositions(int lastMatchStart, int lastMatchEnd, int searchLimit)
lastMatchStart
- the new lastMatchStartPositionlastMatchEnd
- the new lastMatchEnd positionsearchLimit
- the new searchLimit. This will be limited to the source
string length, so you can pass Integer.MAX_VALUE to set it to the string length
explicitly.java.lang.IndexOutOfBoundsException
- if (lastMatchStart < 0 || lastMatchEnd < lastMatchStart || searchLimit < lastMatchEnd)public void reset()
This is equivalent to setPositions(0,0,Integer.MAX_VALUE);
public int getLastMatchStart()
public int getLastMatchEnd()
public java.lang.String getLastMatch()
public <MATCHRESULT> MATCHRESULT findNext(DfaState<MATCHRESULT> state)
The string is searched from getLastMatchEnd() to the search limit to find a substring that matches a pattern in the given DFA.
If there is a match, then the LastMatchStart and LastMatchEnd positions are set to the start and end of the first match, and the MATCHRESULT that the DFA produces for that match is returned.
If there is more than one match starting at the same position, the longest one is selected.
MATCHRESULT
- the type of results produced by the DFAstate
- The start state of the DFA for the patterns you want to findpublic <MATCHRESULT> MATCHRESULT matchAt(DfaState<MATCHRESULT> state, int startPos)
If there is a non-empty match for the DFA in the source string starting at startPos, then the LastMatchStart position is set to startPos, the LastMatchEnd position is set to the end of the longest such match, and the MATCHRESULT from that match is returned.
MATCHRESULT
- the type of results produced by the DFAstate
- The start state of the DFA for the patterns you want to matchstartPos
- the position in the source string to test for a matchpublic static <MATCHRESULT> MATCHRESULT matchWholeString(DfaState<MATCHRESULT> state, java.lang.String str)
MATCHRESULT
- the type of results produced by the DFAstate
- DFA start statestr
- string to test