Search code examples
regexvimmultilineregex-negationinverse

Vim Regex - How to invert a regex which matches multiple lines?


My goal is the regex to match everything but the multi-line matches from the original regex.

This is the regex which I'm trying to invert:

\vselect\_.{-};

It matches correctly on the bellow sample code.
The sql statements are included and not the capitalized text.

THIS TEXT IS WHAT I WANT TO
MATCH ONCE THE ABOVE
REGEX IS CORRECTLY INVERTED.

select *
from foo
where bar;

THIS TEXT IS WHAT I WANT TO
MATCH ONCE THE ABOVE
REGEX IS CORRECTLY INVERTED.

select *
from bar
where foo;

THIS TEXT IS WHAT I WANT TO
MATCH ONCE THE ABOVE
REGEX IS CORRECTLY INVERTED.

Following the vim documentation on how to do invert (negate) a regex, this is what I come up with:

\v^(select\_.{-};)@!.*

This regex matches on everything but on the first lines from the sql statements. I want all lines from the sql statements to be excluded and not just the first lines.

I also tried not to use the \v very magic directive and escaped everything, but no luck, getting the same result.

What am I doing wrong?
Also if anyone can think of another way to accomplish what I'm trying to do would be great, the answer does not have to follow the way I'm trying it.


Solution

  • After thinking it over, I think I got what you meant...

    This line should work for you:

    \v;\n\n\zs\_.{-}\ze\n\_^select
    

    Update according to the comment:

    And this vimregex works if you have leading/ending TEXT blocks:

    \v(;\n\n\zs\_.{-}|%^(select)@!\_.{-})\ze(\n\_^select|\n?%$)