java.util.regex
Class Pattern
java.lang.Object java.util.regex.Pattern
-
All Implemented Interfaces:
- Serializable
public final class Patternextends 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 a Matcher
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 the Matcher
class are not safe for such use.