vim正则表达式和普通正则表达式有什么区别?

时间:2010-10-05 14:12:05

标签: regex linux vim

我注意到vim的替代正则表达式与其他正则表达式略有不同。它们之间有什么区别?

5 个答案:

答案 0 :(得分:29)

如果“普通正则表达式”是指Perl兼容正则表达式(PCRE),那么Vim帮助提供了Vim正则表达式与Perl之间差异的很好的总结:

:help perl-patterns

以下是Vim 7.2的内容:

9. Compare with Perl patterns                           *perl-patterns*

Vim's regexes are most similar to Perl's, in terms of what you can do.  The
difference between them is mostly just notation;  here's a summary of where
they differ:

Capability                      in Vimspeak     in Perlspeak ~
----------------------------------------------------------------
force case insensitivity        \c              (?i)
force case sensitivity          \C              (?-i)
backref-less grouping           \%(atom\)       (?:atom)
conservative quantifiers        \{-n,m}         *?, +?, ??, {}?
0-width match                   atom\@=         (?=atom)
0-width non-match               atom\@!         (?!atom)
0-width preceding match         atom\@<=        (?<=atom)
0-width preceding non-match     atom\@<!        (?!atom)
match without retry             atom\@>         (?>atom)

Vim and Perl handle newline characters inside a string a bit differently:

In Perl, ^ and $ only match at the very beginning and end of the text,
by default, but you can set the 'm' flag, which lets them match at
embedded newlines as well.  You can also set the 's' flag, which causes
a . to match newlines as well.  (Both these flags can be changed inside
a pattern using the same syntax used for the i flag above, BTW.)

On the other hand, Vim's ^ and $ always match at embedded newlines, and
you get two separate atoms, \%^ and \%$, which only match at the very
start and end of the text, respectively.  Vim solves the second problem
by giving you the \_ "modifier":  put it in front of a . or a character
class, and they will match newlines as well.

Finally, these constructs are unique to Perl:
- execution of arbitrary code in the regex:  (?{perl code})
- conditional expressions:  (?(condition)true-expr|false-expr)

...and these are unique to Vim:
- changing the magic-ness of a pattern:  \v \V \m \M
   (very useful for avoiding backslashitis)
- sequence of optionally matching atoms:  \%[atoms]
- \& (which is to \| what "and" is to "or";  it forces several branches
   to match at one spot)
- matching lines/columns by number:  \%5l \%5c \%5v
- setting the start and end of the match:  \zs \ze

答案 1 :(得分:22)

&#34;正则表达&#34;真正定义算法,而不是语法。这意味着不同风格的正则表达式将使用不同的字符来表示相同的东西;或者他们会在一些特殊字符前面添加反斜杠,其他人不会这样做。他们通常仍然以同样的方式工作。

曾几何时,POSIX defined the Basic Regular Expression语法(BRE),Vim很大程度上遵循。不久之后,还发布了扩展正则表达式(ERE)语法提议。两者之间的主要区别在于BRE倾向于将更多字符视为文字 - &#34; a&#34;是&#34; a&#34;,也是&#34;(&#34;是&#34;(&#34;,不是特殊字符 - 所以需要更多的反斜杠来给他们&# 34;特殊&#34;意思。

在这里单独评论Vim和Perl之间复杂差异的讨论是有用的,但它也值得一提Vim正则表达式与接受&#34;接受的一些简单方法。规范(你可能意味着Perl。)如上所述,他们在使用前面的反斜杠时大多不同。

以下是一些明显的例子:

Perl    Vim     Explanation
---------------------------
x?      x\=     Match 0 or 1 of x
x+      x\+     Match 1 or more of x
(xyz)   \(xyz\) Use brackets to group matches
x{n,m}  x\{n,m} Match n to m of x
x*?     x\{-}   Match 0 or 1 of x, non-greedy
x+?     x\{-1,} Match 1 or more of x, non-greedy
\b      \< \>   Word boundaries
$n      \n      Backreferences for previously grouped matches

这可以让您了解最重要的差异。但是,如果您正在做比基础更复杂的事情,我建议您始终假设Vim-regex与Perl-regex或Javascript-regex不同,并咨询类似Vim Regex website的内容。

答案 2 :(得分:4)

尝试Vim非常神奇的正则表达式模式。它的行为更像传统的正则表达式,只是在\v之前添加模式。有关详细信息,请参阅:help /\v。我喜欢它。

答案 3 :(得分:3)

问题太广了。运行vim并输入:help pattern

答案 4 :(得分:3)

有一个名为eregex.vim的插件,它可以从PCRE(Perl兼容的正则表达式)转换为Vim的语法。需要over a thousand lines of vim to achieve that translation!我想它也可以作为差异的精确记录。