Regex to remove text

Dm114

Well-known member
I've got a line by line text from a file, stored in a string variable. It is strctured like this:
START Text1 KEYWORD:Line1 to be removed ... Line n to be removed STOP START Text2 KEYWORD:Line1 to be removed ... Line n to be removed STOP

The goal is to remove every group of lines from KEYWORD to STOP to get such final string:
START Text1 START Text2

Regex KEYWORD.*STOP doesn't work as dot "." doesn't match new lines

Regex KEYWORD[\s\S]*STOP doesn't work either as it matches from the KEYWORD first occurrence to the very last STOP occurrence, removing all intermediate lines.

Does anyone have an idea to achieve that?

Thanks in advance.
 

Endercraft

Moderator (& bug finder :D)
KEYWORD[\s\S]*?STOP
Notice the ?, it renders [\S\s]* non-greedy (or lazy), which means once it encounters KEYWORD, it will start looking for STOP, then when it finds one instead of "being greedy" and searching until it can't find anymore, it's lazy and stops there.
 

Dm114

Well-known member
KEYWORD[\s\S]*?STOP
Notice the ?, it renders [\S\s]* non-greedy (or lazy), which means once it encounters KEYWORD, it will start looking for STOP, then when it finds one instead of "being greedy" and searching until it can't find anymore, it's lazy and stops there.
Thanks a lot. I'll try it ASAP but I think you're right: I had read something like that in the past but I'm not so comfortable with these Regex "specialties"... 😉
 
Top