public class SequenceMatcher<T> extends BasicSequenceMatchResult<T>
Generic sequence matcher
Similar to Java's Matcher
except it matches sequences over an arbitrary type T
instead of characters
For a type T
to be matchable, it has to have a corresponding NodePattern
that indicates
whether a node is matched or not
A matcher is created as follows:
SequencePattern p = SequencePattern.compile("...");
SequencePattern m = p.getMatcher(List sequence);
Functions for searching
boolean matches()
boolean find()
boolean find(int start)
Functions for retrieving matched patterns
int groupCount()
List<T> groupNodes(), List<T> groupNodes(int g)
String group(), String group(int g)
int start(), int start(int g), int end(), int end(int g)
Functions for replacing
List<T> replaceFirst(List<T> seq), List replaceAll(List<T> seq)
List<T> replaceFirstExtended(List<MatchReplacement<T>> seq), List<T> replaceAllExtended(List<MatchReplacement<T>> seq)
Functions for defining the region of the sequence to search over
(default region is entire sequence)
void region(int start, int end)
int regionStart()
int regionEnd()
NOTE: When find is used, matches are attempted starting from the specified start index of the sequence The match with the earliest starting index is returned.
Modifier and Type | Class and Description |
---|---|
static class |
SequenceMatcher.BasicMatchReplacement<T>
Replacement item is a sequence of items
|
static class |
SequenceMatcher.FindType
Type of search to perform
FIND_NONOVERLAPPING - Find nonoverlapping matches (default)
FIND_ALL - Find all potential matches
Greedy/reluctant quantifiers are not enforced
(perhaps should add syntax where some of them are enforced...)
|
static class |
SequenceMatcher.GroupMatchReplacement<T>
Replacement item is a matched group specified with a group id
|
static interface |
SequenceMatcher.MatchReplacement<T>
Interface that specifies what to replace a matched pattern with
|
static class |
SequenceMatcher.NamedGroupMatchReplacement<T>
Replacement item is a matched group specified with a group name
|
BasicSequenceMatchResult.MatchedGroup
SequenceMatchResult.GroupToIntervalFunc<MR extends MatchResult>, SequenceMatchResult.MatchedGroupInfo<T>
DEFAULT_COMPARATOR, GROUP_AFTER_MATCH, GROUP_BEFORE_MATCH, LENGTH_COMPARATOR, OFFSET_COMPARATOR, ORDER_COMPARATOR, PRIORITY_COMPARATOR, PRIORITY_SCORE_LENGTH_ORDER_OFFSET_COMPARATOR, SCORE_COMPARATOR, SCORER, TO_INTERVAL
CONTAINS_FIRST_ENDPOINTS_COMPARATOR, ENDPOINTS_COMPARATOR, LENGTH_ENDPOINTS_COMPARATOR, NESTED_FIRST_ENDPOINTS_COMPARATOR
Modifier | Constructor and Description |
---|---|
protected |
SequenceMatcher(SequencePattern<T> pattern,
List<? extends T> elements) |
Modifier and Type | Method and Description |
---|---|
int |
end(int group) |
boolean |
find()
Searches for the next occurrence of the pattern
|
boolean |
find(int start)
Reset the matcher and then searches for pattern at the specified start index
|
protected boolean |
find(int start,
boolean matchStart) |
Iterable<SequenceMatchResult<T>> |
findAllNonOverlapping()
Applies the matcher and returns all non overlapping matches
|
protected boolean |
findMatchStart(int start,
boolean matchAllTokens) |
protected boolean |
findMatchStartBacktracking(int start,
boolean matchAllTokens) |
protected boolean |
findMatchStartNoBacktracking(int start,
boolean matchAllTokens) |
T |
get(int i)
Returns the ith element
|
SequenceMatcher.FindType |
getFindType() |
SequenceMatchResult.MatchedGroupInfo<T> |
groupInfo(int group) |
Object |
groupMatchResult(int group,
int index)
Returns an Object representing the result for the match for a particular node in a group.
|
List<Object> |
groupMatchResults(int group)
Returns a list of Objects representing the match results for the nodes in the group.
|
List<T> |
groupNodes(int group)
Returns the matched group as a list.
|
Object |
groupValue(int group)
Returns the value (some Object) associated with the captured group.
|
boolean |
isMatchWithResult() |
boolean |
matches()
Checks if the pattern matches the entire sequence
|
Object |
nodeMatchResult(int index)
Returns an Object representing the result for the match for a particular node.
|
void |
region(int start,
int end)
Set region to search in
|
int |
regionEnd() |
int |
regionStart() |
List<T> |
replaceAll(List<T> replacement)
Replaces all occurrences of the pattern with the specified list.
|
List<T> |
replaceAllExtended(List<SequenceMatcher.MatchReplacement<T>> replacement)
Replaces all occurrences of the pattern with the specified list
of replacement items (can include matched groups).
|
List<T> |
replaceFirst(List<T> replacement)
Replaces the first occurrence of the pattern with the specified list.
|
List<T> |
replaceFirstExtended(List<SequenceMatcher.MatchReplacement<T>> replacement)
Replaces the first occurrence of the pattern with the specified list
of replacement items (can include matched groups).
|
void |
reset()
Clears matcher
- Clears matched groups, reset region to be entire sequence
|
void |
setBranchLimit(int blimit) |
void |
setFindType(SequenceMatcher.FindType findType) |
void |
setMatchWithResult(boolean matchWithResult) |
int |
start(int group) |
BasicSequenceMatchResult<T> |
toBasicSequenceMatchResult()
Returns a copy of the current match results.
|
copy, elements, end, end, getInterval, getOrder, group, group, group, groupCount, groupInfo, groupInfo, groupMatchResult, groupMatchResults, groupMatchResults, groupNodes, groupNodes, groupValue, groupValue, pattern, priority, score, setOrder, start, start
protected SequenceMatcher(SequencePattern<T> pattern, List<? extends T> elements)
public void setBranchLimit(int blimit)
public List<T> replaceAllExtended(List<SequenceMatcher.MatchReplacement<T>> replacement)
replacement
- What to replace the matched sequence withreplaceFirst(java.util.List)
,
replaceFirstExtended(java.util.List)
,
replaceAllExtended(java.util.List)
public List<T> replaceFirstExtended(List<SequenceMatcher.MatchReplacement<T>> replacement)
replacement
- What to replace the matched sequence withreplaceFirst(java.util.List)
,
replaceAll(java.util.List)
,
replaceAllExtended(java.util.List)
public List<T> replaceAll(List<T> replacement)
replaceAllExtended(java.util.List)
to replace with matched groups.replacement
- What to replace the matched sequence withreplaceAllExtended(java.util.List)
,
replaceFirst(java.util.List)
,
replaceFirstExtended(java.util.List)
public List<T> replaceFirst(List<T> replacement)
replaceFirstExtended(java.util.List)
to replace with matched groups.replacement
- What to replace the matched sequence withreplaceAll(java.util.List)
,
replaceAllExtended(java.util.List)
,
replaceFirstExtended(java.util.List)
public SequenceMatcher.FindType getFindType()
public void setFindType(SequenceMatcher.FindType findType)
public boolean isMatchWithResult()
public void setMatchWithResult(boolean matchWithResult)
public boolean find(int start)
start
- - Index at which to start the searchIndexOutOfBoundsException
- if start is < 0 or larger then the size of the sequencefind()
protected boolean find(int start, boolean matchStart)
public Iterable<SequenceMatchResult<T>> findAllNonOverlapping()
public boolean find()
find(int)
protected boolean findMatchStart(int start, boolean matchAllTokens)
protected boolean findMatchStartNoBacktracking(int start, boolean matchAllTokens)
protected boolean findMatchStartBacktracking(int start, boolean matchAllTokens)
public boolean matches()
find()
public void region(int start, int end)
start
- - start indexend
- - end index (exclusive)public int regionEnd()
public int regionStart()
public BasicSequenceMatchResult<T> toBasicSequenceMatchResult()
toBasicSequenceMatchResult
in interface SequenceMatchResult<T>
toBasicSequenceMatchResult
in class BasicSequenceMatchResult<T>
public int start(int group)
start
in interface MatchResult
start
in class BasicSequenceMatchResult<T>
public int end(int group)
end
in interface MatchResult
end
in class BasicSequenceMatchResult<T>
public List<T> groupNodes(int group)
SequenceMatchResult
groupNodes
in interface SequenceMatchResult<T>
groupNodes
in class BasicSequenceMatchResult<T>
group
- The index of a capturing group in this matcher's patternpublic Object groupValue(int group)
SequenceMatchResult
groupValue
in interface SequenceMatchResult<T>
groupValue
in class BasicSequenceMatchResult<T>
group
- The index of a capturing group in this matcher's patternpublic SequenceMatchResult.MatchedGroupInfo<T> groupInfo(int group)
groupInfo
in interface SequenceMatchResult<T>
groupInfo
in class BasicSequenceMatchResult<T>
public List<Object> groupMatchResults(int group)
SequenceMatchResult
groupMatchResults
in interface SequenceMatchResult<T>
groupMatchResults
in class BasicSequenceMatchResult<T>
group
- The index of a capturing group in this matcher's patternpublic Object groupMatchResult(int group, int index)
SequenceMatchResult
groupMatchResult
in interface SequenceMatchResult<T>
groupMatchResult
in class BasicSequenceMatchResult<T>
group
- The index of a capturing group in this matcher's patternindex
- The index of the element in the captured subsequence.public Object nodeMatchResult(int index)
SequenceMatchResult
nodeMatchResult
in interface SequenceMatchResult<T>
nodeMatchResult
in class BasicSequenceMatchResult<T>
index
- The index of the element in the original sequence.public void reset()
public T get(int i)
i
- - index