public class SequenceMatcher<T> extends BasicSequenceMatchResult<T>
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<T>
that indicates
whether a node is matched or not.
A matcher is created as follows:
SequencePattern<T> p = SequencePattern<T>.compile("...");
SequencePattern<T> m = p.getMatcher(List<T> 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 java.util.regex.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, LENGTH_GT_COMPARATOR, LENGTH_LT_COMPARATOR, NESTED_FIRST_ENDPOINTS_COMPARATOR
Modifier | Constructor and Description |
---|---|
protected |
SequenceMatcher(SequencePattern<T> pattern,
java.util.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) |
java.lang.Iterable<SequenceMatchResult<T>> |
findAllNonOverlapping()
Applies the matcher and returns all non overlapping matches
|
T |
get(int i)
Returns the ith element
|
SequenceMatcher.FindType |
getFindType() |
SequenceMatchResult.MatchedGroupInfo<T> |
groupInfo(int group) |
java.lang.Object |
groupMatchResult(int group,
int index)
Returns an Object representing the result for the match for a particular node in a group.
|
java.util.List<java.lang.Object> |
groupMatchResults(int group)
Returns a list of Objects representing the match results for the nodes in the group.
|
java.util.List<T> |
groupNodes(int group)
Returns the matched group as a list.
|
java.lang.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
|
java.lang.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() |
java.util.List<T> |
replaceAll(java.util.List<T> replacement)
Replaces all occurrences of the pattern with the specified list.
|
java.util.List<T> |
replaceAllExtended(java.util.List<SequenceMatcher.MatchReplacement<T>> replacement)
Replaces all occurrences of the pattern with the specified list
of replacement items (can include matched groups).
|
java.util.List<T> |
replaceFirst(java.util.List<T> replacement)
Replaces the first occurrence of the pattern with the specified list.
|
java.util.List<T> |
replaceFirstExtended(java.util.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.
|
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, java.util.List<? extends T> elements)
public void setBranchLimit(int blimit)
public java.util.List<T> replaceAllExtended(java.util.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 java.util.List<T> replaceFirstExtended(java.util.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 java.util.List<T> replaceAll(java.util.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 java.util.List<T> replaceFirst(java.util.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 searchjava.lang.IndexOutOfBoundsException
- if start is < 0 or larger then the size of the sequencefind()
protected boolean find(int start, boolean matchStart)
public java.lang.Iterable<SequenceMatchResult<T>> findAllNonOverlapping()
public boolean find()
find(int)
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 java.util.regex.MatchResult
start
in class BasicSequenceMatchResult<T>
public int end(int group)
end
in interface java.util.regex.MatchResult
end
in class BasicSequenceMatchResult<T>
public java.util.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 java.lang.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 java.util.List<java.lang.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 java.lang.Object groupMatchResult(int group, int index)
SequenceMatchResult
Map<Class, Object>
, while
for String, the match result is typically a MatchResult.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 java.lang.Object nodeMatchResult(int index)
SequenceMatchResult
Map<Class, Object>
, while
for String, the match result is typically a MatchResult.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