Vim 如果 char 遵循反向引用,则具有反向引用的正则表达式将失败

Vim regex with back-reference to look-behind fails if a char follows the back-reference

我正在学习 Vim 风格的正则表达式,想了解为什么这不起作用。

假设我想捕获标签之后的所有内容,包括结束标签:

<div>Test div</div>More words
     ^^^^^^^^^^^^^^

这行得通,但去掉了尾随 >:

/\v%(\<(\w+)\>)@<=.*\<\/

所以我希望它能工作,但它什么也没捕获:

/\v%(\<(\w+)\>)@<=.*\<\/\>

我知道还有其他方法可以捕捉到这一点,但我只是想了解为什么我不能在 </code> 反向引用之后包含一个字符。</p> <p>为了方便和我的理解,这是我对正则表达式的理解:</p> <pre><code>/\v %( # non-capturing \< # < ( # captures group 1 \w+ # 1+ alpha-numeric chars ) \> # > )@<= # the match should be preceded by all of the above .* # anything \<\/ # </ # that which was captured as group 1 \> # >

是的,这看起来像是新的基于 NFA 的正则表达式引擎中的错误。当切换到旧引擎时,您必须交换捕获组并使用(所有在 :help /\@<= 下解释),但是匹配有效:

\%#=1\v%()@<=.*\<\/(\w+)\>

也正因为如此,:help 与@PeterRincker 一致认为最好使用 \zs 代替:

\v%(\<(\w+)\>)\zs.*\<\/\>

请报告此错误,请参阅 :help bugs。基本上,您可以通过电子邮件将信息发送到 vim_dev mailing list, or there's also a bug tracker.