Java正则表达式中带有负前瞻断言的奇怪性

我很难理解Java中正则表达式的行为,并遇到了一些看起来很奇怪的东西。 在下面的代码中,测试突然失败,原因是我在测试时不理解消息标签“6个字母匹配,负面”(后续的两个测试也失败了)。 我一直盯着这个太长时间还是确实发生了一些奇怪的事情? 我不是这与可变长度负向前瞻断言(?!X)有关,但我很乐意听到任何理论,甚至是其他人遇到同样问题的确认,并且它并不特定于我的JVM。 对不起,正则表达式是如此做作,但你不想看到真实的东西:)

// $ java -version // java version "1.7.0_10" // Java(TM) SE Runtime Environment (build 1.7.0_10-b18) // Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode) // test of word without agreement String test = "plusieurs personne sont"; // match the pattern with curly braces assertTrue("no letters matched", Pattern.compile("plusieurs personne\\b").matcher(test).find()); assertTrue("1 letters matched", Pattern.compile("plusieurs personn\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("2 letters matched", Pattern.compile("plusieurs person\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("3 letters matched", Pattern.compile("plusieurs perso\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("4 letters matched", Pattern.compile("plusieurs pers\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("5 letters matched", Pattern.compile("plusieurs per\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("6 letters matched", Pattern.compile("plusieurs pe\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("7 letters matched", Pattern.compile("plusieurs p\\p{Alpha}{1,100}\\b").matcher(test).find()); assertTrue("8 letters matched", Pattern.compile("plusieurs \\p{Alpha}{1,100}\\b").matcher(test).find()); // match the negative pattern (without s or x) with curly braces assertTrue("no letters matched, negative", Pattern.compile("plusieurs (?!personne[sx])\\w+").matcher(test).find()); assertTrue("1 letters matched, negative", Pattern.compile("plusieurs (?!personn\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("2 letters matched, negative", Pattern.compile("plusieurs (?!person\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("3 letters matched, negative", Pattern.compile("plusieurs (?!perso\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("4 letters matched, negative", Pattern.compile("plusieurs (?!pers\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("5 letters matched, negative", Pattern.compile("plusieurs (?!per\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); // the assertion below fails (is false) for reasons unknown assertTrue("6 letters matched, negative", Pattern.compile("plusieurs (?!pe\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("7 letters matched, negative", Pattern.compile("plusieurs (?!p\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); assertTrue("8 letters matched, negative", Pattern.compile("plusieurs (?!\\p{Alpha}{1,100}[sx])\\w+").matcher(test).find()); 

让我们看看前瞻是如何匹配的:

 pe literal, matches "pe" r matches \p{Alpha}{1,100} s matches [sx] 

所以否定的前瞻不匹配(你的字符串的尾部, "onne sont" ,这里无关紧要)。

如果您的想法是下一个单词不应以s或x结尾,则在[sx]之后放置\\b可能会有所帮助。 始终要记住,负面前瞻不会对失败感到抱歉 ,并且它不会回溯以便找到如何使你的正则表达式不匹配

UPD:让我们仔细观察案例5,将其与案例6进行比较。这里我们使用假设匹配(对于前瞻内部的表达式),因此我们必须考虑几种可能(几乎)发生的变体。

 per literal, would match "per" -- it's always so -- let's try to imagine how the rest could match: sonn would match \p{Alpha}{1,100} e wouldn't match [sx], FAIL -- or maybe s would match \p{Alpha}{1,100} o wouldn't match [sx], FAIL -- or maybe yet so would match \p{Alpha}{1,100} n wouldn't match [sx], FAIL. 

如果第二个词是“个人化”,我们将有另一个有趣的冒险。

UPD2:评论中的讨论促使我在这里添加一个概括:

正则表达式很有吸引力,因为它们具有人类思维的重要特征: 确认偏差 。 当我们编写正则表达式时,我们希望它们匹配; 即使我们的工作是防止无效输入,我们也会在大多数时候考虑有效输入。 正则表达式匹配器通常共享此属性:它匹配并且讨厌失败。 这就是为什么像\p{Alpha}{1,100}这样的子表达式并不意味着“在尝试匹配剩余的输入之前,吃掉最长的Alpha数据块”。 它粗略地意味着“考虑长度在[1,100]内的所有可能的Alpha块,找到一种方法让整个表达式匹配”。

这就是为什么使用则表达式时,很容易忽略复杂表达式的误报 :错误接受的无效输入。 这个问题没有出现,当我们使用负向前瞻时它变得更加明显

在负面预测中,regexp matcher 想要匹配内部表达式(使外部表达式失败)。 人类程序员仍然希望匹配外部表达; 正如我们在我们的例子中看到的,这个因素确实会影响我们对内在表达的推理。 我们认为它不应该如此难以匹配 (例如,它应该以愚蠢的方式处理子表达式,立即吃掉最长的输入)。 匹配器通常起作用,但我们对理想行为想法现在与其算法不同步。 内在表达的误报(很难注意到)会成为外部表达的错误否定(我们注意到并且讨厌)。