import re
regex = re.compile(r"\r?\n(?!\r?\n)")
test_str = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit\n"
" sed do eiusmod tempor incididunt ut labore et dolore magna\n"
"aliqua. Ut enim ad minim veniam, quis nostrud exercitation \n\n\n"
"ullamco laboris nisi ut aliquip ex ea \n"
"commodo consequat. Duis aute irure dolor \n"
"in reprehenderit in voluptate velit esse \n\n\n"
"cillum dolore eu fugiat nulla pariatur. \n"
"Excepteur sint occaecat cupidatat")
subst = ""
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