6

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

Vineeth
  • 473
  • 1
  • 6
  • 8

3 Answers3

10

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.

too much php
  • 88,666
  • 34
  • 128
  • 138
3

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

:perldo s/search/replace/
Todd Hunter
  • 871
  • 1
  • 8
  • 21
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)

Chad Birch
  • 73,098
  • 23
  • 151
  • 149