gpt4 book ai didi

java - 关于 requireEnd Matcher 方法的说明

转载 作者:行者123 更新时间:2023-12-04 04:49:44 25 4
gpt4 key购买 nike

直接来自 this Java API:

public boolean requireEnd()

Returns true if more input could change a positive match into a negative one.

If this method returns true, and a match was found, then more input could cause the match to be lost. If this method returns false and a match was found, then more input might change the match but the match won't be lost. If a match was not found, then requireEnd has no meaning.

Returns: true iff more input could change a positive match into a negative one.



我在网上找不到任何具体的例子。

有人可以向我展示它的使用示例吗?
提前致谢。

最佳答案

让我们以这个简单的例子为例:

public static void main(final String... args)
{
final Pattern p = Pattern.compile("cat$");

final Matcher m = p.matcher("I have a cat");
m.find(); // finds a match
System.out.println(m.requireEnd()); // prints true
}

正则表达式的末尾有一个 anchor ;文档说,如果给出更多输入,它“[...] 可能会导致匹配丢失”。的确。考虑这个输入:
"I have a catflap"

正则表达式在这里不再匹配:匹配丢失。

如果现在我们替换 "cat$""cat"然后 .requireEnd()会返回 false :使用上面的输入, cat仍然会被正则表达式引擎匹配。

关于java - 关于 requireEnd Matcher 方法的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17618812/

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