Search code examples
regexpcre2

Remove qualifying lines of text from string including its leading newline or trailing newline, but not both


In the following regex:

EXCLUDE this entire line
include this line
and this as single match
and EXCLUDE this line

I want to return a single match consisting for two lines:

include this line
and this as single match

I want to use EXCLUDE as string identifying that the entire line should not be included.

edit: if I can get just the first match up to the line with "EXCLUDE" (or end of document whichever happens first), that would work too


Solution

  • With pcre you can use \K to fotget what is matched so far, and first match the line containing exclude:

    ^.*\bEXCLUDE\b.*\K(?:\R(?!.*\bEXCLUDE\b).*)+
    

    Regex demo

    If you want to match all lines that do not contain exclude, with consecutive lines:

    (?:(?:^|\R)(?!.*\bEXCLUDE\b).*)+
    

    Regex demo

    Or using a skip fail approach:

    ^.*\bEXCLUDE\b.*\R(*SKIP)(*F)|.+(?:\R(?!.*\bEXCLUDE\b).*)*
    

    Regex demo