forX
forX

Reputation: 2153

regex split by block filter by content

I want to remove every bloc starting by X, finishing by Y and containing value Z

http://regex101.com/r/jS7eA5

Text:

BEGIN
DESCRIPTION: 1234
BOOL:TRUE
END
BEGIN
DESCRIPTION: 4568
BOOL:TRUE
END
BEGIN
DESCRIPTION: 715310
BOOL:FALSE
END
BEGIN
DESCRIPTION: 12489453
BOOL:TRUE
END
BEGIN
DESCRIPTION: 41543
BOOL:FALSE
END

Result :

BEGIN
DESCRIPTION: 1234
BOOL:TRUE
END
BEGIN
DESCRIPTION: 4568
BOOL:TRUE
END
BEGIN
DESCRIPTION: 12489453
BOOL:TRUE
END

Here, we want to remove every bloc starting by "BEGIN", ENDING BY "END", WITH "BOOL:FALSE" INSIDE. another point of view : I want to keep every bloc starting by "BEGIN", ENDING BY "END", WITH "BOOL:TRUE" INSIDE.

This one take the first begin and searche the first bool:false before search the end. That's not right. (?s)(BEGIN.?BOOL:FALSE.?END)

What I want its like using a filter on

(?s)(BEGIN.*?BOOL\:FALSE.*?END)

filter by BOOL:FALSE

(?s)(BEGIN.*?BOOL\:FALSE.*?END)[.FILTERBY_BOOL:FALSE

Upvotes: 1

Views: 48

Answers (1)

Jerry
Jerry

Reputation: 71568

You can use a negative lookahead for this:

BEGIN((?!TRUE).)*?END

This checks for every dot that you have whether there's not TRUE ahead, until END. So, all the other blocks without TRUE will be removed.

You can also insert a [\r\n]? to make things a little cleaner:

[\r\n]?BEGIN((?!TRUE).)*?END

Updated the regex101.

EDIT: If you want to only pinpoint FALSE, you can use this regex, which is a bit longer:

[\r\n]?BEGIN(?:(?!FALSE|END).)*?FALSE(?:(?!FALSE|END).)*?END

Updated regex101 with this one.

Upvotes: 1

Related Questions