public final class Matcher extends Object
Pattern on a
specific input. Its interface mimics the JDK 1.4.2
java.util.regex.Matcher.
Conceptually, a Matcher consists of four parts:
Pattern, set at
construction and fixed for the lifetime of the matcher.reset() and advanced by each match operation such as
find(), matches() or lookingAt().start(),
end(), and group(), and updated by each match
operation.appendReplacement(io.airlift.slice.SliceOutput, io.airlift.slice.Slice) and appendTail(io.airlift.slice.SliceOutput) if performing a
search and replace from the input to an external StringBuffer.
See the package-level documentation for an overview of how to use this API.
| Modifier and Type | Method and Description |
|---|---|
Matcher |
appendReplacement(io.airlift.slice.SliceOutput so,
io.airlift.slice.Slice replacement)
Appends to
so two slices: the text from the append position up
to the beginning of the most recent match, and then the replacement with
submatch groups substituted for references of the form $n, where
n is the group number in decimal. |
io.airlift.slice.SliceOutput |
appendTail(io.airlift.slice.SliceOutput so)
Appends to
so the subslice of the input from the
append position to the end of the input. |
int |
end()
Returns the end position of the most recent match.
|
int |
end(int group)
Returns the end position of a subgroup of the most recent match.
|
boolean |
find()
Matches the input against the pattern (unanchored).
|
boolean |
find(int start)
Matches the input against the pattern (unanchored),
starting at a specified position.
|
io.airlift.slice.Slice |
group()
Returns the most recent match.
|
io.airlift.slice.Slice |
group(int group)
Returns the subgroup of the most recent match.
|
int |
groupCount()
Returns the number of subgroups in this pattern.
|
boolean |
lookingAt()
Matches the beginning of input against the pattern (anchored start).
|
boolean |
matches()
Matches the entire input against the pattern (anchored start and end).
|
Pattern |
pattern()
Returns the
Pattern associated with this Matcher. |
io.airlift.slice.Slice |
replaceAll(io.airlift.slice.Slice replacement)
Returns the input with all matches replaced by
replacement,
interpreted as for appendReplacement. |
io.airlift.slice.Slice |
replaceFirst(io.airlift.slice.Slice replacement)
Returns the input with the first match replaced by
replacement,
interpreted as for appendReplacement. |
Matcher |
reset()
Resets the
Matcher, rewinding input and
discarding any match information. |
Matcher |
reset(io.airlift.slice.Slice input)
Resets the
Matcher and changes the input. |
int |
start()
Returns the start position of the most recent match.
|
int |
start(int group)
Returns the start position of a subgroup of the most recent match.
|
public Pattern pattern()
Pattern associated with this Matcher.public Matcher reset()
Matcher, rewinding input and
discarding any match information.Matcher itself, for chained method callspublic Matcher reset(io.airlift.slice.Slice input)
Matcher and changes the input.input - the new input SliceMatcher itself, for chained method callspublic int start()
IllegalStateException - if there is no matchpublic int end()
IllegalStateException - if there is no matchpublic int start(int group)
group - the group index; 0 is the overall matchIllegalStateException - if there is no matchIndexOutOfBoundsException - if group < 0 or group > groupCount()public int end(int group)
group - the group index; 0 is the overall matchIllegalStateException - if there is no matchIndexOutOfBoundsException - if group < 0 or group > groupCount()public io.airlift.slice.Slice group()
IllegalStateException - if there is no matchpublic io.airlift.slice.Slice group(int group)
IllegalStateException - if there is no matchIndexOutOfBoundsException - if group < 0
or group > groupCount()public int groupCount()
public boolean matches()
matches sets the match state to describe it.public boolean lookingAt()
lookingAt sets the match state to describe it.public boolean find()
find sets the match state to describe it.public boolean find(int start)
find sets the match state to describe it.start - the input position where the search beginsIndexOutOfBoundsException - if start is not a valid input positionpublic Matcher appendReplacement(io.airlift.slice.SliceOutput so, io.airlift.slice.Slice replacement)
so two slices: the text from the append position up
to the beginning of the most recent match, and then the replacement with
submatch groups substituted for references of the form $n, where
n is the group number in decimal. It advances the append position
to the position where the most recent match ended.
To embed a literal $, use \$ (actually "\\$" with string
escapes). The escape is only necessary when $ is followed by a
digit, but it is always allowed. Only $ and \ need
escaping, but any character can be escaped.
The group number n in $n is always at least one digit
and expands to use more digits as long as the resulting number is a
valid group number for this pattern. To cut it off earlier, escape the
first digit that should not be used.
so - the SliceOutput to append toreplacement - the replacement SliceMatcher itself, for chained method callsIllegalStateException - if there was no most recent matchIndexOutOfBoundsException - if replacement refers to an invalid groupIllegalArgumentException - if replacement has incorrect formatpublic io.airlift.slice.SliceOutput appendTail(io.airlift.slice.SliceOutput so)
so the subslice of the input from the
append position to the end of the input.so - the SliceOutput to append toso, for method chainingpublic io.airlift.slice.Slice replaceAll(io.airlift.slice.Slice replacement)
replacement,
interpreted as for appendReplacement.replacement - the replacement SliceSlice with the matches replacedIndexOutOfBoundsException - if replacement refers to an invalid grouppublic io.airlift.slice.Slice replaceFirst(io.airlift.slice.Slice replacement)
replacement,
interpreted as for appendReplacement.replacement - the replacement SliceSlice with the first match replacedIndexOutOfBoundsException - if replacement refers to an invalid groupCopyright © 2016. All Rights Reserved.