Class Matcher
- java.lang.Object
-
- java.util.regex.Matcher
-
-
All Implemented Interfaces:
- MatchResult
public final class Matcher extends Object implements MatchResult
An engine(引擎) thatperforms(执行) matchoperations(操作) on acharacter sequence
byinterpreting(解释) a
Pattern
.A matcher is created from a pattern by invoking the pattern's
matcher
method. Once created, a matcher can be used to perform threedifferent(不同的)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 orfailure(失败). Moreinformation(信息) 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 theregion. By default, theregion(区域)contains(包含) all of the matcher's input. The region can be modified(修改的) via通过) the
region
method andqueried(查询的)via theregionStart
andregionEnd
methods. The way that the regionboundaries(边界)interact(交互) with some patternconstructs(构造) can be changed. SeeuseAnchoringBounds
anduseTransparentBounds
for moredetails(详细资料).This class also defines(定义) methods for replacing matched subsequences with new strings whose contents can, if desired(需要), becomputed(计算) from the match result. The
appendReplacement
andappendTail
methods can be used in tandem(一前一后地) in order tocollect(收集) the result into anexisting(现存的) 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 mostrecent(最近的) successful match. It also includes the start and end indices of the input subsequence captured(捕获) by eachcapturing 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 theappend 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(多线程并发).