vim 正则表达式和普通正则表达式有什么区别? [英] What's the difference between vim regex and normal regex?

查看:28
本文介绍了vim 正则表达式和普通正则表达式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

:help perl-patterns

以下是 Vim 7.2 中的内容:

<前>9. 与 Perl 模式比较 *perl-patterns*Vim 的正则表达式与 Perl 的最相似,就您可以做什么而言.这它们之间的区别主要是符号;这是哪里的摘要它们不同:Perlspeak 中 Vimspeak 中的能力 ~----------------------------------------------------------------强制不区分大小写 c (?i)强制区分大小写 C (?-i)无反向引用分组 \%(atom) (?:atom)保守量词 {-n,m} *?, +?, ??, {}?0-宽度匹配原子@= (?=atom)0 宽度不匹配原子@!(?!原子)0-宽度前面的匹配原子@<= (?<=atom)0-宽度前面的不匹配原子@(?>原子)Vim 和 Perl 处理字符串中的换行符有点不同:在 Perl 中,^ 和 $ 仅匹配文本的开头和结尾,默认情况下,但您可以设置m"标志,让它们匹配嵌入换行符也是如此.您还可以设置 's' 标志,这会导致一个 .也匹配换行符.(这两个标志都可以在里面更改使用与上面 i 标志相同的语法的模式,顺便说一句.)另一方面,Vim 的 ^ 和 $ 总是在嵌入的换行符处匹配,并且你会得到两个单独的原子,\%^ 和 \%$,它们只匹配最分别是文本的开头和结尾.Vim 解决了第二个问题通过给你 \_ 修饰符":把它放在 .或一个字符类,它们也会匹配换行符.最后,这些结构是 Perl 独有的:- 在正则表达式中执行任意代码:(?{perl code})- 条件表达式:(?(condition)true-expr|false-expr)...这些是 Vim 独有的:- 改变模式的魔力:v V m M(对于避免反斜线炎非常有用)- 可选匹配原子的序列:\%[atoms]- &(这是|什么是和"是或";它强制几个分支在一处匹配)- 按数字匹配行/列:\%5l \%5c \%5v- 设置比赛的开始和结束:zs ze

I noticed that vim's substitute regex is a bit different from other regexp. What's the difference between them?

解决方案

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:

:help perl-patterns

Here's what it says as of 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

这篇关于vim 正则表达式和普通正则表达式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆