Class Matcher
- All Implemented Interfaces:
MatchResult
,MatchResult
Pattern
. This is a wrapper for Matcher
.- Since:
- 0.1.9
-
Method Summary
Modifier and TypeMethodDescriptionappendReplacement
(StringBuffer sb, String replacement) Implements a non-terminal append-and-replace step.Implements a terminal append-and-replace step.int
end()
Returns the offset after the last character matched.int
end
(int group) Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.int
Returns the offset after the last character of the subsequence captured by the given named group during the previous match operation.boolean
boolean
find()
Attempts to find the next subsequence of the input sequence that matches the pattern.boolean
find
(int start) Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.group()
Returns the input subsequence matched by the previous match.group
(int group) Returns the input subsequence captured by the given group during the previous match operation.Returns the input subsequence captured by the named group during the previous match operation.int
Returns the number of capturing groups in this matcher's pattern.boolean
Queries the anchoring of region bounds for this matcher.int
hashCode()
boolean
Queries the transparency of region bounds for this matcher.boolean
hitEnd()
Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.boolean
Attempts to match the input sequence, starting at the beginning of the region, against the pattern.boolean
matches()
Attempts to match the entire region against the pattern.Returns a map of the pattern's named groups and indexes within the pattern.Finds all named groups that exist in the input string.Returns the named pattern that is interpreted by this matcher.Gets a list of the matches in the order in which they occur in a matching input stringstatic String
Returns a literal replacementString
for the specifiedString
.region
(int start, int end) Sets the limits of this matcher's region.int
Reports the end index (exclusive) of this matcher's region.int
Reports the start index of this matcher's region.replaceAll
(String replacement) Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.replaceFirst
(String replacement) Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.boolean
Returns true if more input could change a positive match into a negative one.reset()
Resets this matcherreset
(CharSequence input) Resets this matcher with a new input sequenceReturns the pattern that is interpreted by this matcher.int
start()
Returns the start index of the previous match.int
start
(int group) Returns the start index of the subsequence captured by the given group during the previous match operation.int
Returns the start index of the subsequence captured by the given named group during the previous match operation.Returns the match state of this matcher as a MatchResult.toString()
useAnchoringBounds
(boolean b) Sets the anchoring of region bounds for this matcher.usePattern
(Pattern newPattern) Changes the Pattern that this Matcher uses to find matches withuseTransparentBounds
(boolean b) Sets the transparency of region bounds for this matcher.
-
Method Details
-
standardPattern
Returns the pattern that is interpreted by this matcher.- Returns:
- the pattern
-
namedPattern
Returns the named pattern that is interpreted by this matcher.- Returns:
- the pattern
-
usePattern
Changes the Pattern that this Matcher uses to find matches with- Parameters:
newPattern
- the new pattern- Returns:
- this Matcher
-
reset
Resets this matcher- Returns:
- this Matcher
-
reset
Resets this matcher with a new input sequenceResetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected
- Parameters:
input
- The new input character sequence- Returns:
- this Matcher
-
matches
public boolean matches()Attempts to match the entire region against the pattern.If the match succeeds then more information can be obtained via the start, end, and group methods.
- Returns:
true
if, and only if, the entire region sequence matches this matcher's pattern
-
toMatchResult
Returns the match state of this matcher as a MatchResult. The result is unaffected by subsequent operations performed upon this matcher.- Returns:
- a MatchResult with the state of this matcher
-
find
public boolean find()Attempts to find the next subsequence of the input sequence that matches the pattern.This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
If the match succeeds then more information can be obtained via the start, end, and group methods.
- Returns:
true
if, and only if, a subsequence of the input sequence matches this matcher's pattern
-
find
public boolean find(int start) Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.If the match succeeds then more information can be obtained via the start, end, and group methods, and subsequent invocations of the find() method will start at the first character not matched by this match.
- Parameters:
start
- the starting index- Returns:
true
if, and only if, a subsequence of the input sequence starting at the given index matches this matcher's pattern- Throws:
IndexOutOfBoundsException
- If start is less than zero or if start is greater than the length of the input sequence.
-
lookingAt
public boolean lookingAt()Attempts to match the input sequence, starting at the beginning of the region, against the pattern.Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.
If the match succeeds then more information can be obtained via the start, end, and group methods.
- Returns:
true
if, and only if, a prefix of the input sequence matches this matcher's pattern
-
appendReplacement
Implements a non-terminal append-and-replace step.- Parameters:
sb
- The target string bufferreplacement
- The replacement string- Returns:
- The target string buffer
-
quoteReplacement
Returns a literal replacementString
for the specifiedString
. This method produces aString
that will work as a literal replacements
in theappendReplacement
method of theMatcher
class. TheString
produced will match the sequence of characters ins
treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.- Parameters:
s
- The string to be literalized- Returns:
- A literal string replacement
- Since:
- 0.2.7
-
appendTail
Implements a terminal append-and-replace step.- Parameters:
sb
- The target string buffer- Returns:
- The target string buffer
-
group
Returns the input subsequence matched by the previous match.- Specified by:
group
in interfaceMatchResult
- Returns:
- The (possibly empty) subsequence matched by the previous match, in string form
-
group
Returns the input subsequence captured by the given group during the previous match operation.- Specified by:
group
in interfaceMatchResult
- Parameters:
group
- The index of a capturing group in this matcher's pattern- Returns:
- the subsequence
- Throws:
IllegalStateException
- If no match has yet been attempted, or if the previous match operation failed
-
groupCount
public int groupCount()Returns the number of capturing groups in this matcher's pattern.- Specified by:
groupCount
in interfaceMatchResult
- Returns:
- The number of capturing groups in this matcher's pattern
-
orderedGroups
Gets a list of the matches in the order in which they occur in a matching input string- Specified by:
orderedGroups
in interfaceMatchResult
- Returns:
- the matches
-
group
Returns the input subsequence captured by the named group during the previous match operation.- Specified by:
group
in interfaceMatchResult
- Parameters:
groupName
- name of the capture group- Returns:
- the subsequence
- Throws:
IndexOutOfBoundsException
- if group name not found
-
namedGroups
Returns a map of the pattern's named groups and indexes within the pattern.- Specified by:
namedGroups
in interfaceMatchResult
- Returns:
- an unmodifiable map of group names to 1-based indexes (empty if named groups not found). Example: pattern: (a)(b)(?<group1>x)(c)(?<group2>y) output: {"group1": 3, "group2": 5}
- Since:
- 1.0.0
-
namedGroupsList
Finds all named groups that exist in the input string. This resets the matcher and attempts to match the input against the pre-specified pattern.- Specified by:
namedGroupsList
in interfaceMatchResult
- Returns:
- a list of maps, each containing name-value matches (empty if no match found). Example: pattern: (?<dote>\d+).(?<day>\w+) input: 1 Sun foo bar 2 Mon foo output: [{"date":"1", "day":"Sun"}, {"date":"2", "day":"Mon"}]
- Since:
- 1.0.0
-
start
public int start()Returns the start index of the previous match.- Specified by:
start
in interfaceMatchResult
- Returns:
- the start index
-
start
public int start(int group) Returns the start index of the subsequence captured by the given group during the previous match operation.- Specified by:
start
in interfaceMatchResult
- Parameters:
group
- the index of the capture group- Returns:
- the index
-
start
Returns the start index of the subsequence captured by the given named group during the previous match operation.- Specified by:
start
in interfaceMatchResult
- Parameters:
groupName
- the name of the capture group- Returns:
- the index
-
end
public int end()Returns the offset after the last character matched.- Specified by:
end
in interfaceMatchResult
- Returns:
- the offset
-
end
public int end(int group) Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.- Specified by:
end
in interfaceMatchResult
- Parameters:
group
- the index of the capture group- Returns:
- the offset
-
end
Returns the offset after the last character of the subsequence captured by the given named group during the previous match operation.- Specified by:
end
in interfaceMatchResult
- Parameters:
groupName
- the name of the capture group- Returns:
- the offset
-
region
Sets the limits of this matcher's region.- Parameters:
start
- The index to start searching at (inclusive)end
- The index to end searching at (exclusive)- Returns:
- this Matcher
-
regionEnd
public int regionEnd()Reports the end index (exclusive) of this matcher's region. The searches this matcher conducts are limited to finding matches within regionStart (inclusive) and regionEnd (exclusive).- Returns:
- the ending point of this matcher's region
-
regionStart
public int regionStart()Reports the start index of this matcher's region. The searches this matcher conducts are limited to finding matches within regionStart (inclusive) and regionEnd (exclusive).- Returns:
- The starting point of this matcher's region
-
hitEnd
public boolean hitEnd()Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.- Returns:
- true iff the end of input was hit in the last match; false otherwise
-
requireEnd
public boolean requireEnd()Returns true if more input could change a positive match into a negative one.- Returns:
- true iff more input could change a positive match into a negative one.
-
hasAnchoringBounds
public boolean hasAnchoringBounds()Queries the anchoring of region bounds for this matcher.- Returns:
- true iff this matcher is using anchoring bounds, false otherwise.
-
hasTransparentBounds
public boolean hasTransparentBounds()Queries the transparency of region bounds for this matcher.- Returns:
- true iff this matcher is using transparent bounds, false otherwise
-
replaceAll
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.- Parameters:
replacement
- The replacement string- Returns:
- The string constructed by replacing each matching subsequence by the replacement string, substituting captured subsequences as needed
-
replaceFirst
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.- Parameters:
replacement
- The replacement string- Returns:
- The string constructed by replacing the first matching subsequence by the replacement string, substituting captured subsequences as needed
-
useAnchoringBounds
Sets the anchoring of region bounds for this matcher.- Parameters:
b
- a boolean indicating whether or not to use anchoring bounds.- Returns:
- this Matcher
-
useTransparentBounds
Sets the transparency of region bounds for this matcher.- Parameters:
b
- a boolean indicating whether to use opaque or transparent regions- Returns:
- this Matcher
-
equals
-
hashCode
public int hashCode() -
toString
-