Conditional regex in vim?

别来无恙 提交于 2020-01-10 19:57:20

问题


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


回答1:


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.




回答2:


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

:perldo s/search/replace/



回答3:


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)



来源:https://stackoverflow.com/questions/2901240/conditional-regex-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!