Search code examples
regexpcresubstitution

Regex match expected but not matching


Given this string of ; delimited values


a;; z
toy;d;hh 
toy
;b;;jj
z;
d;23
d;23td
;;io;
b y;b;12
z
a;b;bb;;;34
z

and this regex

^(?!([^\r\n;]*);(?:(b|d))(?:;|$)).*\R

I am looking to get the full lines whose 2nd. column is not b or d, and have the matching lines removed, like this

toy;d;hh 
;b;;jj
b y;b;12
a;b;bb;;;34

Please see the demo

Line 13's second column is not b or d. Still, it does not match and it shows in the substitution box.

Line 11 has the same string and it does match.

Any help is appreciated


Solution

  • That's because you're looking for a new line sequence \R that doesn't exist at the end of the last line.

    using one the following can fix the issue:

    ^(?!([^\r\n;]*);(?:(b|d))(?:;|$)).*\R*

    ^(?!([^\r\n;]*);(?:(b|d))(?:;|$)).*(\R|$)

    ^(?!([^\r\n;]*);(?:(b|d))(?:;|$)).*\R{0,1}

    What Chnaged?

    \R* will match zero OR more new line sequences

    \R{0,1} will match zero OR one new line sequence

    (\R|$) will match a new line sequence OR end of line