vim 正则表达式和普通正则表达式有什么区别?
我注意到 vim 的替代正则表达式与其他正则表达式有点不同。他们之间有什么区别?
I noticed that vim's substitute regex is a bit different from other regexp. What's the difference between them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“正则表达式”真正定义的是算法,而不是语法。这意味着不同风格的正则表达式将使用不同的字符来表示相同的内容;或者他们会在一些特殊字符前加上反斜杠,而其他字符则不会。它们通常仍以相同的方式工作。
曾几何时,POSIX 定义了基本正则表达式语法 (BRE),Vim 在很大程度上使用了该语法接下来。不久之后,扩展正则表达式(ERE)语法提案也发布了。两者之间的主要区别在于,BRE 倾向于将更多字符视为文字 - “a” 是“a”,但“(”也是“(”,而不是特殊字符 - 因此涉及更多反斜杠给它们“特殊”的含义。
在这里单独评论讨论 Vim 和 Perl 之间的复杂差异是有用的,但也值得一提的是 Vim 正则表达式与“公认”规范不同的一些更简单的方式。可能是指 Perl。)如上所述,它们的主要区别在于前面的反斜杠的使用。
下面是一些明显的例子
:我建议您始终假设 Vim-regex 与 Perl-regex 或 Javascript-regex 不同,并参考 Vim Regex 网站之类的内容。
"Regular expression" really defines algorithms, not a syntax. What that means is that different flavours of regular expressions will use different characters to mean the same thing; or they'll prefix some special characters with backslashes where others don't. They'll typically still work in the same way.
Once upon a time, POSIX defined the Basic Regular Expression syntax (BRE), which Vim largely follows. Very soon afterwards, an Extended Regular Expression (ERE) syntax proposal was also released. The main difference between the two is that BRE tends to treat more characters as literals - an "a" is an "a", but also a "(" is a "(", not a special character - and so involves more backslashes to give them "special" meaning.
The discussion of complex differences between Vim and Perl on a separate comment here is useful, but it's also worth mentioning a few of the simpler ways in which Vim regexes differ from the "accepted" norm (by which you probably mean Perl.) As mentioned above, they mostly differ in their use of a preceding backslash.
Here are some obvious examples:
That gives you a flavour of the most important differences. But if you're doing anything more complicated than the basics, I suggest you always assume that Vim-regex is going to be different from Perl-regex or Javascript-regex and consult something like the Vim Regex website.
如果“正常正则表达式”指的是 Perl 兼容正则表达式 (PCRE),那么 Vim 帮助提供了 Vim 正则表达式和 Perl 正则表达式之间差异的良好总结:
以下是 Vim 7.2 中的内容:
If by "normal regex" you mean Perl-Compatible Regular Expressions (PCRE), then the Vim help provides a good summary of the differences between Vim's regexes and Perl's:
Here's what it says as of Vim 7.2:
尝试 Vim 非常神奇的正则表达式模式。它的行为更像传统的正则表达式,只需在模式前面加上
\v
即可。请参阅:help /\v
了解更多信息。我喜欢它。Try Vim's very magic regex mode. It behaves more like traditional regex, just prepend your pattern with
\v
. See:help /\v
for more info. I love it.有一个名为 eregex.vim 的插件,它是从 PCRE (Perl-兼容的正则表达式)到 Vim 的语法。 需要一千多行 vim 才能实现该翻译!我想它也可以作为差异的精确记录。
There is a plugin called eregex.vim which translates from PCRE (Perl-compatible regular expressions) to Vim's syntax. It takes over a thousand lines of vim to achieve that translation! I guess it also serves as precise documentation of the differences.
太宽泛的问题。运行 vim 并输入
:help pattern
。Too broad question. Run vim and type
:help pattern
.