Matcher

本文详细介绍了Java中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(引擎) thatperforms(执行) matchoperations(操作) on acharacter sequence byinterpreting(解释) aPattern.

    A matcher is created from a pattern by invoking the pattern'smatcher 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通过) theregion method andqueried(查询的)via theregionStart and regionEnd 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. TheappendReplacement 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(造成) anIllegalStateException 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 theappendReplacement method.

    A matcher may be reset(重置) explicitly by invoking itsreset() 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(多线程并发).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值