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

gc0ot86w  于 2022-11-22  发布在  Linux
关注(0)|答案(5)|浏览(240)

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

vjhs03f7

vjhs03f71#

“正则表达式”真正定义的是算法,而不是语法。这意味着不同风格的正则表达式将使用不同的字符来表示同一件事;或者在某些特殊字符前加上反斜杠,而其他字符则没有。它们通常仍以相同的方式工作。
很久以前,POSIX定义了基本正则表达式语法(BRE),Vim主要遵循它。不久之后,一个扩展的正则表达式(ERE)语法提案也发布了,两者的主要区别在于BRE倾向于将更多的字符视为文字-一个“a”是一个“a”,但也有一个“(“是一个“(“,而不是特殊字符-因此需要更多的反斜杠来给予它们“特殊”的含义。
在这里的单独注解中讨论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或Javascript-regex,并参考类似Vim Regex website的东西。

hgqdbh6s

hgqdbh6s2#

如果您所说的“普通正则表达式”是指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
pw136qt2

pw136qt23#

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

0md85ypi

0md85ypi4#

有一个叫做eregex.vim的插件,它可以将PCRE(Perl兼容的正则表达式)翻译成Vim的语法。它使用over a thousand lines of vim to achieve that translation!我猜它也可以作为差异的精确文档。

2guxujil

2guxujil5#

问题太宽泛。运行vim并键入:help pattern

相关问题