Matcher的matches()方法返回false,find()和groupCount()返回结果 [英] Matcher's matches() method returns false, find() and groupCount() return results

查看:364
本文介绍了Matcher的matches()方法返回false,find()和groupCount()返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

private static final String ET_AL = "(\\p{Punct}|\\b|^|\\s)et\\.? al\\.?(\\b|$|\\s)";
private static final Pattern ET_AL_PATTERN = Pattern.compile(ET_AL, Pattern.CASE_INSENSITIVE);

...

Matcher m = ET_AL_PATTERN.matcher("Doe, J.; Brown, C.; et al");
while(m.find()) {
    System.out.println(m.group());
}
m.reset();
System.out.println(m.matches());

循环打印et al但是对m.matches()的调用返回false。 m.groupCount()还会返回2.有关正在发生的事情的任何帮助吗?

The loop prints "et al" but the call to m.matches() returns false. Also m.groupCount() returns 2. Any help about what is going on?

推荐答案


但是对m.matches()的调用返回false。

but the call to m.matches() returns false.

这是因为 Matcher#matches() 要求您将完整输入与正则表达式匹配。

It is because Matcher#matches() requires you to match complete input with your regex.

来自Javadoc:


公共布尔匹配()

尝试将整个区域与模式匹配。如果匹配
成功,则可以通过开始,结束和
组方法获得更多信息。

Attempts to match the entire region against the pattern. If the match succeeds then more information can be obtained via the start, end, and group methods.

这篇关于Matcher的matches()方法返回false,find()和groupCount()返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆