Serhiy Storchaka added the comment: Atomic groups can help you: '<div>((?><p>.*?</p>))</div>'.
But this feature is not supported in the re module yet (see issue433030). You can use the third-party regex module which is compatible with the re module and supports atomic grouping. >>> import regex as re >>> pattern = re.compile('<div>((?><p>.*?</p>))</div>', flags=re.DOTALL) >>> print(re.sub(pattern, '\\1', ... '<div><p>foo</p>123456789</div>\n' ... '<div><p>bar</p></div>\n')) <div><p>foo</p>123456789</div> <p>bar</p> ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue30720> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com