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(任意的) character sequences against(依照) the regular expression. All of the state involved in performing(执行) 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 an expression 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 for such 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'smatcher method. 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 theregion. By default, the region(范围) contains all of the matcher's input. The region can be modified via(通过) theregion method and queried via theregionStart and regionEnd 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 in tandem(串联) in order to collect the result into an existing string buffer, or the more convenientreplaceAll 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 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.
本文介绍了Java中正则表达式的编译与匹配过程。详细解释了Pattern和Matcher类的作用,包括模式编译、创建匹配器、执行不同类型的匹配操作(如matches、lookingAt和find方法),以及如何替换匹配到的子序列。
2010

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



