Class Matcher
- java.lang.Object
-
- java.util.regex.Matcher
-
- All Implemented Interfaces:
- MatchResult
public final class Matcher extends Object implements MatchResult
An engine(引擎) that performs(执行) match operations on a character sequence by interpreting(解释) aPattern
.A matcher is created from a pattern by invoking the pattern's
matcher
method. Once created, a matcher can be used to perform three different kinds of match operations:-
The
matches
method attempts(试图) to match the entire(全部) input sequence against(违反) the pattern. -
The
lookingAt
method attempts to match the input sequence, starting at the beginning, against the pattern. -
The
find
method scans(扫描) the input sequence looking for the next subsequence that matches the pattern.
Each of these methods returns a boolean indicating(表明) success or failure. More information about a successful match can be obtained(获得) by querying the state of the matcher.
A matcher finds matches in a subset of its input called the region. By default, the region contains all of the matcher's input. The region can be modified via the
region
method and queried via theregionStart
andregionEnd
methods. The way that the region boundaries(边界) interact(互相作用) with some pattern constructs(构造) can be changed. SeeuseAnchoringBounds
anduseTransparentBounds
for more details.This class also defines methods for replacing matched subsequences with new strings whose contents can, if desired(需求), be computed from the match result. The
appendReplacement
andappendTail
methods can be used in tandem(串联) in order to collect(汇集) the result into an existing string buffer, or the more convenient(方便的)replaceAll
method can be used to create a string in which every matching subsequence in the input sequence is replaced.The explicit(明确的) state of a matcher includes the start and end indices(目录) of the most recent(最近的) successful match. It also includes the start and end indices of the input subsequence captured(捕获) by each capturing group in the pattern as well as(和…一样) a total count of such(这样的) subsequences. As a convenience(方便), methods are also provided for returning these captured(捕获) subsequences in string form(格式).
The explicit(明确的) state of a matcher is initially(最初的) undefined; attempting(试图) to query any part of it before a successful match will cause(导致) an
IllegalStateException
to be thrown. The explicit(明确的) state of a matcher is recomputed(验算) by every match operation.The implicit(隐式) state of a matcher includes the input character sequence as well as the append position, which is initially(最初的) zero and is updated by the
appendReplacement
method.A matcher may be reset explicitly(明确的) by invoking its
reset()
method or, if a new input sequence is desired(需求), itsreset(CharSequence)
method. Resetting a matcher discards(废弃物) its explicit(明确的) state information and sets the append position to zero.Instances of this class are not safe for use by multiple concurrent threads.