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

gui*_* 桂林 26 regex linux vim

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

Bil*_*dom 32

如果通过"正常正则表达式"表示Perl兼容正则表达式(PCRE),则Vim帮助提供了Vim正则表达式与Perl之间差异的良好总结:

:help perl-patterns
Run Code Online (Sandbox Code Playgroud)

以下是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


J-P*_*J-P 23

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

曾几何时,POSIX定义了基本正则表达式语法(BRE),Vim很大程度上遵循这种语法.不久之后,还发布了扩展正则表达式(ERE)语法提议.两者之间的主要区别是,BRE倾向于把更多的字符作为文字-一个"一"是"一",又是"("是"(",而不是一个特殊字符-因此涉及到更多的反斜杠给他们"特殊"的含义.

在这里单独评论Vim和Perl之间复杂差异的讨论是有用的,但是值得一提的是Vim正则表达式与"接受"规范(你可能意味着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网站这样的东西.


Evg*_*eev 5

有一个名为eregex.vim的插件,它可以将 PCRE(Perl 兼容的正则表达式)转换为 Vim 的语法。需要一千多行 vim 才能实现该翻译!我想它也可以作为差异的精确记录。


but*_*bat 5

试试Vim非常神奇的regex模式。它的行为更像传统的正则表达式,只是在模式之前加上\v。请参阅:help /\v以获取更多信息。我喜欢它。