Define a paragraph as a multi-line string delimited on both side with double new lines ('\n\n'). if there exist a paragraph which contains a certain string ('BAD'), i want to replace that paragraph (i.e. any text containing BAD up to the closest preceding and following double newlines) with some other token ('GOOD'). this should be with a python 3 regex.
i have text such as:
Here you are:
OK, to break it down a little (because it's nasty looking):
\n\nmatches two literal line breaks.
(?:[^\n]|\n(?!\n))*is a non-capturing group that matches either a single non-line break character, or a line break character that isn't followed by another. We repeat the entire group 0 or more times (in case
BADappears at the beginning of the paragraph).
BADwill match the literal text you want. Simple enough.
Then, you just replace it with
\n\nGOOD, and you're off to the races.