java.util.regex.Pattern
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.
特殊字符

另外补充 /b , 字符边界符。
here is an example, /bhere/b 可以匹配 here
adhere to our plan, /bhere/b 不能匹配 here,因为它不是一个单独的单词
本文介绍了Java中正则表达式的应用,包括Pattern类的基本概念、如何创建Pattern实例及使用Matcher对象进行字符串匹配。同时,文章提供了示例代码帮助理解。
2586

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



