Let's say you have some HTML like this:
<pre><div>inline test</div></pre>
or this:
<pre>
<p>
multi
line
test
</p>
</pre>
but you want escaped HTML like this:
<pre><div>inline test</div></pre>
<pre><p>
multi
line
test
</p></pre>
The following code takes a string and replaces the contents of each pre tag with escaped HTML:
import re
import html
text = """<pre><div>inline test</div></pre>
<pre>
<p>
multi
line
test
</p>
</pre>
"""
pattern = "<pre>\n?((.|\n)*?)\n?</pre>"
result = re.sub(pattern, lambda x: "<pre>" + html.escape(x.group(1)) + "</pre>", text)
print(result)