import re
regex = re.compile(r"""
(?m)
(?:
^ \d{2} - \d{4} [^|\r\n]* \|
| \G
)
(?: [^|\r\n]* \| )*
\K
[\r\n]+ (?! [\r\n]* (?: ^ \d{2} - \d{4} | $ ) )
""", flags=re.VERBOSE)
test_str = ("00-1234T|\n"
"Data|Commments|\n"
"12-3456|Some data|Notes|\n"
"65-8436ZZ|Data|\n"
"|\n"
"45-4576AA|Some data|Comments|\n"
"98-4392REV|Data|\n"
"|\n"
"00-5432|Some Data|Some Comments|\n")
subst = "#CRLF#"
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