I have a multi-line string and I'd like to only extract lines that match a certain pattern and discard the rest of the string.
Original string:
lorem ipsum dolor sit amet consectetur
nunc KEEP THIS LINE ut massa lorem
DO NOT KEEP THIS LINE aenean blandit
nunc KEEP THIS LINE et justo quis
praesent at velit felis vel
Desired final string:
KEEP THIS LINE ut massa lorem
KEEP THIS LINE et justo quis
The regex I have so far is:
(?ms).*?(?-s)^nunc (KEEP THIS LINE.*?$)
I can just replace the match with the backreference \1. However, this fails to replace from the end of the last matched pattern to the end of the string.
The regex:
(?ms).*?(?-s)(^nunc (KEEP THIS LINE.*?$)|(?s:).*?\Z)
successfully identifies the pattern between the last desired line and the end of the string, but I can't figure out how to remove it.
