vim:使用perl兼容的正则表达式进行交互式搜索和替换

时间:2020-12-22 16:49:38

According to this page you can use perl compatible regular expression with

根据此页面,您可以使用perl兼容的正则表达式

:perldo s/pattern/insert/g.

:perldo s / pattern / insert / g。

This works fine.

这很好用。

But, how can I get interactive search and replace with PCRE syntax in vim?

但是,如何在vim中进行交互式搜索并替换为PCRE语法?

Since this does not work with perldo I search for a different solution.

由于这不适用于perldo我搜索不同的解决方案。

4 个解决方案

#1


5  

Till the current release version of vim, there is no way to do :s/[perlRegex]/bar/c

直到当前发布的vim版本,没有办法:s / [perlRegex] / bar / c

So you are asking for a feature that doesn't exist.

所以你要求的是一个不存在的功能。

You can do matching with verymagic, however it is not Perl Regex compatible flag. It is still using the vimregex engine, just changed the way of escaping regex patterns.

你可以与verymagic匹配,但它不是Perl Regex兼容的标志。它仍在使用vimregex引擎,只是改变了逃避正则表达式模式的方式。

For example, in perl, you can do lookahead/behind (?<=foo)..., (?=foo), (?!foo).., you can use the handy \K : som.*ing\Kwhatever etc, you cannot use those syntax in vim directly, no matter which 'magic' level you have set. Vim has the same feature, but different syntax:

例如,在perl中,你可以做lookahead / behind(?<= foo)...,(?= foo),(?!foo)..,你可以使用方便的\ K:som。* ing \ Kwhatever等等,你无法直接在vim中使用这些语法,无论你设置了哪个'魔术'级别。 Vim具有相同的功能,但语法不同:

\@=
\@!
\@<=

and also the \zs \ze are very handy, even more powerful than perl's \K.

并且\ zs \ ze非常方便,甚至比perl的\ K更强大。

Vim is an Editor, with vim regex, you can not only do text matching, but also match base on visual selection, cursor position and so on.

Vim是一个编辑器,使用vim正则表达式,你不仅可以进行文本匹配,还可以根据视觉选择,光标位置等进行匹配。

If you really need to do complex pattern matching and really need do them in vim, learn vim regex! It is not difficult for you if you "know pcre very well"

如果你真的需要进行复杂的模式匹配,并且确实需要在vim中进行,请学习vim正则表达式!如果你“非常了解pcre”就不难了

#2


1  

Probably the closest you can get is:

可能距离最近的是:

:s/\vfoo/bar/gc

#3


0  

In your example, s/pattern/insert/g is a perl command, not a Vim command using a perl-compatible regular expression syntax.

在您的示例中,s / pattern / insert / g是perl命令,而不是使用perl兼容的正则表达式语法的Vim命令。

If perl doesn't have an equivalent of Vim's /c flag you will need to find an alternate method like… writing an actual perl script.

如果perl没有等效的Vim / c标志,你将需要找到一个替代方法,如...编写一个实际的perl脚本。

#4


0  

I suggest to try eregex plugin. It maps perl compatible regular expressions to VIM regex syntax.

我建议尝试使用eregex插件。它将perl兼容的正则表达式映射到VIM正则表达式语法。

#1


5  

Till the current release version of vim, there is no way to do :s/[perlRegex]/bar/c

直到当前发布的vim版本,没有办法:s / [perlRegex] / bar / c

So you are asking for a feature that doesn't exist.

所以你要求的是一个不存在的功能。

You can do matching with verymagic, however it is not Perl Regex compatible flag. It is still using the vimregex engine, just changed the way of escaping regex patterns.

你可以与verymagic匹配,但它不是Perl Regex兼容的标志。它仍在使用vimregex引擎,只是改变了逃避正则表达式模式的方式。

For example, in perl, you can do lookahead/behind (?<=foo)..., (?=foo), (?!foo).., you can use the handy \K : som.*ing\Kwhatever etc, you cannot use those syntax in vim directly, no matter which 'magic' level you have set. Vim has the same feature, but different syntax:

例如,在perl中,你可以做lookahead / behind(?<= foo)...,(?= foo),(?!foo)..,你可以使用方便的\ K:som。* ing \ Kwhatever等等,你无法直接在vim中使用这些语法,无论你设置了哪个'魔术'级别。 Vim具有相同的功能,但语法不同:

\@=
\@!
\@<=

and also the \zs \ze are very handy, even more powerful than perl's \K.

并且\ zs \ ze非常方便,甚至比perl的\ K更强大。

Vim is an Editor, with vim regex, you can not only do text matching, but also match base on visual selection, cursor position and so on.

Vim是一个编辑器,使用vim正则表达式,你不仅可以进行文本匹配,还可以根据视觉选择,光标位置等进行匹配。

If you really need to do complex pattern matching and really need do them in vim, learn vim regex! It is not difficult for you if you "know pcre very well"

如果你真的需要进行复杂的模式匹配,并且确实需要在vim中进行,请学习vim正则表达式!如果你“非常了解pcre”就不难了

#2


1  

Probably the closest you can get is:

可能距离最近的是:

:s/\vfoo/bar/gc

#3


0  

In your example, s/pattern/insert/g is a perl command, not a Vim command using a perl-compatible regular expression syntax.

在您的示例中,s / pattern / insert / g是perl命令,而不是使用perl兼容的正则表达式语法的Vim命令。

If perl doesn't have an equivalent of Vim's /c flag you will need to find an alternate method like… writing an actual perl script.

如果perl没有等效的Vim / c标志,你将需要找到一个替代方法,如...编写一个实际的perl脚本。

#4


0  

I suggest to try eregex plugin. It maps perl compatible regular expressions to VIM regex syntax.

我建议尝试使用eregex插件。它将perl兼容的正则表达式映射到VIM正则表达式语法。