szymcio32
11/6/2019 - 4:44 PM

Non greedy regex

import re

PARAGRAPHS_RE = re.compile(r'''
    <p>
    ( . +? )
    </p>
''', re.VERBOSE)

text = "<p>Text with one paragraph</p><p>And another</p>"
text = PARAGRAPHS_RE.sub(r'\1', text)

# output Text with one paragraph And another
# .+? means that it will work as non greedy
# http://pycon2017.regex.training/more-re-module.html#greediness