Pattern.compile()是正则匹配必经步骤,需复用Pattern实例避免重复编译;Matcher非线程安全且须每次新建;find()、matches()、lookingAt()语义不同,group()索引从1起,使用前须确认匹配成功。
直接 new Pattern() 不行,Pattern 没有公开构造函数。所有正则编译都得走 Pattern.compile(),它返回一个可复用的 Pattern 实例。频繁调用却没缓存的话,会重复编译、浪费 CPU。
常见错误:在循环里反复写 Pattern.compile("a+b") —— 应该提出来复用:
Pattern pattern = Pattern.compile("a+b");
for (String s : strings) {
Matcher m = pattern.matcher(s);
if (m.find()) { ... }
}
Matcher 不是线程安全的,也不能跨字符串复用。同一个 Matcher 实例调用 m.reset("new text") 可以重置输入,但更推荐每次用 pattern.matcher(input) 获取新实例——语义清晰、不易出错。
容易踩的坑:
Matcher 当单例或静态字段存着,结果多线程下匹配错乱m.find() 后没重置就直接 m.group(),抛 IllegalStateException
m.find() 之间没检查返回值,导致 group() 访问越界
nd()、matches()、lookingAt() 的行为差异很关键三者都触发匹配,但语义完全不同:
m.find():找子串,只要输入中**存在符合模式的连续片段**就返回 true(比如 "abc123def".matcher("\\d+").find() → true)m.matches():要求**整个输入完全匹配**模式(等价于 ^...$ 包裹),"123".matcher("\\d+").matches() → true,但 "a123".matcher("\\d+").matches() → falsem.lookingAt():只匹配**开头部分**,不要求全串匹配,也不要求后续无字符("123abc".matcher("\\d+").lookingAt() → true)选错方法会导致逻辑 bug,尤其是校验场景误用 find() 替代 matches()。
m.group(0) 是整个匹配内容,m.group(1) 才是第一个捕获组。如果正则里没写括号,m.group(1) 会抛 IndexOutOfBoundsException。
安全做法:
m.groupCount() 看有多少捕获组Matcher,必须在 find() 或 matches() 返回 true 后再调 group()
m.group()(无参)代替 m.group(0),更直观典型错误:Pattern.compile("(\\d+)-(\\w+)").matcher("123-a").find(); String key = m.group(2); —— 这里 group(2) 是合法的;但如果正则写成 "\\d+-\\w+"(没括号),group(1) 就非法了。