直接来自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/