续:
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.
译:
正则表达式的编译表示。
必须先将指定为字符串的正则表达式编译为此类的实例。然后,可以使用所得的模式来创建Matcher对象,该对象可以将任意字符序列与正则表达式进行匹配。进行匹配所涉及的所有状态都位于匹配器中,因此许多匹配器可以共享同一模式。
因此,典型的调用顺序为
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();*
此类定义了matchs方法,以方便仅使用一次正则表达式的情况。此方法编译一个表达式,并在一次调用中将一个输入序列与其匹配。该声明
boolean b = Pattern.matches("a*b", “aaaaab”);
与上面的三个语句等效,但是对于重复匹配而言,效率较低,因为它不允许重新使用已编译的模式。
此类的实例是不可变的,可以安全地由多个并发线程使用。 Matcher类的实例不安全用于此类用途。
方法: