Package com.google.re2j
Class Matcher
java.lang.Object
com.google.re2j.Matcher
A stateful iterator that interprets a regex
Pattern on a
specific input. Its interface mimics the JDK 1.4.2
java.util.regex.Matcher.
Conceptually, a Matcher consists of four parts:
- A compiled regular expression
Pattern, set at construction and fixed for the lifetime of the matcher. - The remainder of the input string, set at construction or
reset()and advanced by each match operation such asfind(),matches()orlookingAt(). - The current match information, accessible via
start(),end(), andgroup(), and updated by each match operation. - The append position, used and advanced by
appendReplacement(io.airlift.slice.SliceOutput, io.airlift.slice.Slice)andappendTail(io.airlift.slice.SliceOutput)if performing a search and replace from the input to an externalStringBuffer.
See the package-level documentation for an overview of how to use this API.
- Author:
- rsc@google.com (Russ Cox)
-
Method Summary
Modifier and TypeMethodDescriptionappendReplacement(io.airlift.slice.SliceOutput so, io.airlift.slice.Slice replacement) Appends tosotwo 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, wherenis the group number in decimal.io.airlift.slice.SliceOutputappendTail(io.airlift.slice.SliceOutput so) Appends tosothe subslice of the input from the append position to the end of the input.intend()Returns the end position of the most recent match.intend(int group) Returns the end position of a subgroup of the most recent match.booleanfind()Matches the input against the pattern (unanchored).booleanfind(int start) Matches the input against the pattern (unanchored), starting at a specified position.io.airlift.slice.Slicegroup()Returns the most recent match.io.airlift.slice.Slicegroup(int group) Returns the subgroup of the most recent match.intReturns the number of subgroups in this pattern.booleanMatches the beginning of input against the pattern (anchored start).booleanmatches()Matches the entire input against the pattern (anchored start and end).pattern()Returns thePatternassociated with thisMatcher.io.airlift.slice.SlicereplaceAll(io.airlift.slice.Slice replacement) Returns the input with all matches replaced byreplacement, interpreted as forappendReplacement.io.airlift.slice.SlicereplaceFirst(io.airlift.slice.Slice replacement) Returns the input with the first match replaced byreplacement, interpreted as forappendReplacement.reset()Resets theMatcher, rewinding input and discarding any match information.reset(io.airlift.slice.Slice input) Resets theMatcherand changes the input.intstart()Returns the start position of the most recent match.intstart(int group) Returns the start position of a subgroup of the most recent match.
-
Method Details
-
pattern
Returns thePatternassociated with thisMatcher. -
reset
Resets theMatcher, rewinding input and discarding any match information.- Returns:
- the
Matcheritself, for chained method calls
-
reset
Resets theMatcherand changes the input.- Parameters:
input- the new inputSlice- Returns:
- the
Matcheritself, for chained method calls
-
start
public int start()Returns the start position of the most recent match.- Throws:
IllegalStateException- if there is no match
-
end
public int end()Returns the end position of the most recent match.- Throws:
IllegalStateException- if there is no match
-
start
public int start(int group) Returns the start position of a subgroup of the most recent match.- Parameters:
group- the group index; 0 is the overall match- Throws:
IllegalStateException- if there is no matchIndexOutOfBoundsException- ifgroup < 0orgroup > groupCount()
-
end
public int end(int group) Returns the end position of a subgroup of the most recent match.- Parameters:
group- the group index; 0 is the overall match- Throws:
IllegalStateException- if there is no matchIndexOutOfBoundsException- ifgroup < 0orgroup > groupCount()
-
group
public io.airlift.slice.Slice group()Returns the most recent match.- Throws:
IllegalStateException- if there is no match
-
group
public io.airlift.slice.Slice group(int group) Returns the subgroup of the most recent match.- Throws:
IllegalStateException- if there is no matchIndexOutOfBoundsException- ifgroup < 0orgroup > groupCount()
-
groupCount
public int groupCount()Returns the number of subgroups in this pattern.- Returns:
- the number of subgroups; the overall match (group 0) does not count
-
matches
public boolean matches()Matches the entire input against the pattern (anchored start and end). If there is a match,matchessets the match state to describe it.- Returns:
- true if the entire input matches the pattern
-
lookingAt
public boolean lookingAt()Matches the beginning of input against the pattern (anchored start). If there is a match,lookingAtsets the match state to describe it.- Returns:
- true if the beginning of the input matches the pattern
-
find
public boolean find()Matches the input against the pattern (unanchored). The search begins at the end of the last match, or else the beginning of the input. If there is a match,findsets the match state to describe it.- Returns:
- true if it finds a match
-
find
public boolean find(int start) Matches the input against the pattern (unanchored), starting at a specified position. If there is a match,findsets the match state to describe it.- Parameters:
start- the input position where the search begins- Returns:
- true if it finds a match
- Throws:
IndexOutOfBoundsException- if start is not a valid input position
-
appendReplacement
public Matcher appendReplacement(io.airlift.slice.SliceOutput so, io.airlift.slice.Slice replacement) Appends tosotwo 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, wherenis 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
nin$nis 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.- Parameters:
so- theSliceOutputto append toreplacement- the replacementSlice- Returns:
- the
Matcheritself, for chained method calls - Throws:
IllegalStateException- if there was no most recent matchIndexOutOfBoundsException- if replacement refers to an invalid groupIllegalArgumentException- if replacement has incorrect format
-
appendTail
public io.airlift.slice.SliceOutput appendTail(io.airlift.slice.SliceOutput so) Appends tosothe subslice of the input from the append position to the end of the input.- Parameters:
so- theSliceOutputto append to- Returns:
- the argument
so, for method chaining
-
replaceAll
public io.airlift.slice.Slice replaceAll(io.airlift.slice.Slice replacement) Returns the input with all matches replaced byreplacement, interpreted as forappendReplacement.- Parameters:
replacement- the replacementSlice- Returns:
- the input
Slicewith the matches replaced - Throws:
IndexOutOfBoundsException- if replacement refers to an invalid group
-
replaceFirst
public io.airlift.slice.Slice replaceFirst(io.airlift.slice.Slice replacement) Returns the input with the first match replaced byreplacement, interpreted as forappendReplacement.- Parameters:
replacement- the replacementSlice- Returns:
- the input
Slicewith the first match replaced - Throws:
IndexOutOfBoundsException- if replacement refers to an invalid group
-