import re
regex = re.compile(r"<p>((?:(?!</?blockquote).)*?)</p>(?=(?:(?!</?blockquote).)*</blockquote>)", flags=re.DOTALL)
test_str = ("<blockquote>\n"
" <p>paragraph 1</p>\n"
"</blockquote>\n"
"<p>paragraph 1 outside blockquote</p>\n"
"<blockquote>\n"
" <p>paragraph 2</p>\n"
" <p>paragraph 3</p>\n"
"</blockquote>\n"
"<p>paragraph 2 outside blockquote</p>")
subst = "<div>\\1</div>"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html