import re
regex = re.compile(r"(?s)^.{0,1000}DELETE ME(?-s).*\R?", flags=re.MULTILINE)
test_str = ("unnecessary stuff\n"
"unnecessary stuff\n"
"DELETE ME and some more stuff\n"
"This line should be the beginning\n"
"the rest.")
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