gpt4 book ai didi

java - Matcher的matches()方法返回false,find()和groupCount()返回结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:34 27 4
gpt4 key购买 nike

我有以下代码:

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。对发生的事情有什么帮助吗?

最佳答案

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

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

来自 Javadoc:

public boolean matches()

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.

关于java - Matcher的matches()方法返回false,find()和groupCount()返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21339594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com