Regex(Pattern、Matcher)

public final class Pattern
extends Object
implements Serializable
A compiled(编译) representation(表示) of a regular expression(正则表达式).

A regular expression, specified as a string, must first be compiled into(编译成) an instance of this class. The resulting pattern can then be used to create aMatcher object that can match arbitrary(任意)charactersequencesagainst(根据) the regular expression. All of the state involved(涉及) inperforming(执行) a match resides(存在) in the matcher, so many matchers can share the same pattern.

A typical(典型的) invocation sequence is thus

 Pattern p = Pattern.compile("a*b");
 Matcher m = p.matcher("aaaaab");
 boolean b = m.matches();

A matches method is defined by this class as a convenience(方便) for when a regular expression is used just once. This method compiles anexpression(表达式) and matches an input sequence against it in a single invocation(调用). The statement

 boolean b = Pattern.matches("a*b", "aaaaab");
is equivalent to the three statements above(上面), though for repeated(重复) matches it is less efficient since(因为) it does not allow the compiled pattern to be reused(重用,再用).

Instances of this class are immutable and are safe for use by multiple concurrent(同时的,并发) threads. Instances of theMatcher class are not safe forsuch use(这样使用).



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 performthree different kinds(三种不同) of match operations:

  • The matches method attempts(试图,尝试) to match theentire(全部的,整个) 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 sequencelooking 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(称为) theregion(区域,范围). By default, the region contains all of the matcher's input. The region can be modifiedvia(通过,经由) theregion method andqueried(询问) 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. TheappendReplacement andappendTail methods can be used intandem(串联) in order to collect(收集) the result into an existing(现存的,目前的) string buffer, or the moreconvenient(方便的)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 subsequencecaptured(捕获的) 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 theappendposition(位置), 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 matcherdiscards(丢弃,放弃) its explicit state information and sets the append position to zero.

Instances of this class are not safe for use by multiple concurrent threads(多个并发线程).


你可能感兴趣的:(Pattern)