Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 什么';vim正则表达式和正常正则表达式的区别是什么?_Regex_Linux_Vim - Fatal编程技术网

Regex 什么';vim正则表达式和正常正则表达式的区别是什么?

Regex 什么';vim正则表达式和正常正则表达式的区别是什么?,regex,linux,vim,Regex,Linux,Vim,我注意到vim的替代正则表达式与其他正则表达式有点不同。他们之间有什么区别?这个问题太宽泛了。运行vim并键入:帮助模式如果“普通正则表达式”指的是Perl兼容正则表达式(PCRE),那么vim帮助提供了vim正则表达式和Perl正则表达式之间差异的良好摘要: :help perl-patterns 以下是Vim 7.2中的内容: 9. Compare with Perl patterns *perl-patterns* Vim's reg

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

这个问题太宽泛了。运行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 9比较Perl模式*Perl模式* Vim的正则表达式与Perl的正则表达式在功能上最为相似。这个 它们之间的区别主要是符号;这里是一个地方的总结 它们不同: Perlspeak中的Vimspeak功能~ ---------------------------------------------------------------- 强制案例不敏感\c(?i) 强制区分大小写\C(?-i) 无反向参考分组\%(原子\)(?:原子) 保守量词\{n,m}*?,+?,?,{}? 0-宽度匹配原子\@=(?=原子) 0-宽度不匹配原子\@!(?!原子) “正则表达式”真正定义的是算法,而不是语法。这意味着不同风格的正则表达式将使用不同的字符来表示同一事物;或者他们会在一些特殊字符前加上反斜杠,而其他字符则不会。它们通常仍然以相同的方式工作

曾几何时,Vim主要遵循的语法(BRE)。不久之后,一个扩展正则表达式(ERE)语法提案也发布了。两者之间的主要区别在于BRE倾向于将更多的字符视为文字——“a”是“a”,但也有“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 Perl-Vim解释 --------------------------- x?x\=匹配x中的0或1 x+x\+匹配一个或多个x (xyz)\(xyz\)使用括号对匹配项进行分组 x{n,m}x\{n,m}匹配x的n到m x*?x\{-}匹配x的0或1,非贪婪 x+?x\{-1,}匹配x中的1个或多个,非贪婪 \b\<\>单词边界 $n\n以前分组的匹配项的反向引用
这让您了解了最重要的区别。但是如果您要做的事情比基础更复杂,我建议您始终假设Vim regex将不同于Perl regex或Javascript regex,并参考类似的内容。

有一个名为的插件,它从PCRE翻译而来(Perl兼容的正则表达式)到Vim的语法。它需要!我想它也可以作为差异的精确文档。

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

定义“普通正则表达式”。每个regex引擎至少与几乎所有其他引擎都有细微的不同。这是最好的答案,帮助我准确地找到了我想要的东西。谢谢。vimregex站点的链接很棒…尽管它可能需要更改为“更新”版本,因为它上一次更新是在2002年。我想你的意思是:x*?x\{-}匹配0个或多个x,非greedBy传统正则表达式您是指基本正则表达式(BRE)还是PCRE?PCRE:)更多文本为什么使用
:help/\v
而不是
:help\v
?斜杠
/
的作用是什么?事实上,这太棒了!我也喜欢它!太糟糕了,这不能成为默认值?请参阅,但有缺点。文档中嵌入新行的
含义是什么?