Python regex to replace a double newline delimited paragraph containing a string
<p>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.</p>

<p>i have text such as:</p>

<pre><code>dfsdf\n
sdfdf\n
\n
blablabla\n
blaBAD\n
bla\n
\n
dsfsdf\n
sdfdf
</code></pre>

<p>should be:</p>

<pre><code>dfsdf\n
sdfdf\n
\n
GOOD\n
\n
dsfsdf\n
sdfdf
</code></pre>

<p>Firstly, you're mixing actual newlines and <code>'\n'</code> characters in your example, I assume that you only meant either. Secondly, let me challenge your assumption that you need regex for this:</p>

<pre><code>inp = '''dfsdf
sdadf

blablabla
blaBAD
bla

dsfsdf
sdfdf'''

replaced = '\n\n'.join(['GOOD' if 'BAD' in k else k for k in inp.split('\n\n')])
</code></pre>

<p>The result is</p>

<pre><code>print(replaced)
'dfsdf\nsdadf\n\nGOOD\n\ndsfsdf\nsdfdf'
</code></pre>

python regex