Java Matcher 翻译

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

java.util.regex

Class 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(解释) 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 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 the regionStart and regionEnd methods. The way that the region boundaries(边界) interact(互相作用) with some pattern constructs(构造) can be changed. See useAnchoringBounds and useTransparentBounds 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 and appendTail 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(需求), its reset(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. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值