加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 19:04:07 所属栏目:安全 来源:网络整理
导读:我注意到vim的替代正则表达式与其他正则表达式略有不同。它们之间有什么区别? 如果通过“正常正则表达式”表示Perl兼容正则表达式(PCRE),则Vim帮助提供了Vim正则表达式与Perl之间差异的良好总结: :help perl-patterns 这是Vim 7.2所说的内容: 9. Compare
我注意到vim的替代正则表达式与其他正则表达式略有不同。它们之间有什么区别?
如果通过“正常正则表达式”表示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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读