澄清requireEnd Matcher的方法 [英] Clarification about requireEnd Matcher's method

查看:75
本文介绍了澄清requireEnd Matcher的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接来自这个 Java API:

Directly from this java API:

布尔布尔requireEnd()

public boolean requireEnd()

如果更多输入可以将正匹配转换为a,则返回true. 负面的.

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

如果此方法返回true,并且找到匹配项,则输入更多 可能导致比赛失败.如果此方法返回false,则返回 找到匹配项,则更多输入可能会更改匹配项,但匹配项 不会丢失.如果找不到匹配项,则requireEnd没有 意义.

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.

我无法在线找到任何具体示例.

I have not been able to find any concrete example on line.

有人可以给我看一个使用它的例子吗? 预先感谢.

Could somebody show me an example of its use? Thanks in advance.

推荐答案

让我们举一个简单的例子:

Let us take this simple example:

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
}

正则表达式的末尾有一个锚点;该文档说,如果给出更多输入,则可能导致匹配失败".的确.考虑一下此输入:

The regex has an anchor at the end; the documentation says that if more input was given, it "[...]could cause the match to be lost". Indeed. consider this input:

"I have a catflap"

这里的正则表达式不再匹配:匹配失败.

The regex does not match anymore here: the match is lost.

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

If now we replace "cat$" with "cat" then .requireEnd() would return false: with the input above, cat would still be matched by the regex engine.

这篇关于澄清requireEnd Matcher的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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