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
matchermethod. Once created, a matcher can be used to perform three different kinds of match operations:-
The
matchesmethod attempts(试图) to match the entire(全部) input sequence against(违反) the pattern. -
The
lookingAtmethod attempts to match the input sequence, starting at the beginning, against the pattern. -
The
findmethod 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
regionmethod and queried via theregionStartandregionEndmethods. The way that the region boundaries(边界) interact(互相作用) with some pattern constructs(构造) can be changed. SeeuseAnchoringBoundsanduseTransparentBoundsfor 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
appendReplacementandappendTailmethods can be used in tandem(串联) in order to collect(汇集) the result into an existing string buffer, or the more convenient(方便的)replaceAllmethod 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
IllegalStateExceptionto 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
appendReplacementmethod.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.

本文详细介绍Java中的Matcher类,包括其核心功能与使用方法。Matcher类是执行正则表达式匹配操作的主要工具,提供了matches、lookingAt及find等方法来完成不同类型的匹配任务,并支持通过替换操作修改字符串。
2103

被折叠的 条评论
为什么被折叠?



