开发者

Conditional regex in vim?

开发者 https://www.devze.com 2022-12-31 06:29 出处:网络
Is it possible to do conditional regex 开发者_JAVA百科(like the one described in http://www.regular-expressions.info/conditional.html) in Vim?Vim regex does not have this feature, so you will need to

Is it possible to do conditional regex 开发者_JAVA百科(like the one described in http://www.regular-expressions.info/conditional.html) in Vim?


Vim regex does not have this feature, so you will need to use a bit of repetition to create the same behaviour:

/\(\%(condition\)\@=then\|\%(condition\)\@!else\)

Note that you have to use the condition twice in the Vim version and the lookahead/lookbehind must always be the opposite in the then/else parts otherwise your regex will not be correct.


Not natively, however if you have +perl in vim you should be able to use

:perldo s/search/replace/


The vim docs state that vim's regexes don't support the conditional expressions (in a section comparing vim's pattern support with perl's):

Finally, these constructs are unique to Perl:
- execution of arbitrary code in the regex: (?{perl code})
- conditional expressions: (?(condition)true-expr|false-expr)

0

精彩评论

暂无评论...
验证码 换一张
取 消