import re
regex = re.compile(r"(?m)(?|^(\d+):()()|\G^()([-+]?\d+)(?![:\d])[ \t]+(.*)|\G^()()()[ \t]*$\R*)\R?")
test_str = ("6:\n"
"1 my 1st log\n"
"-2 my 2nd log\n\n"
"7:\n"
"-3 my 3rd log\n"
"4 my 4th log\n"
"-5 my 5th log\n")
subst = "$1\\t$2\\t$3\\r\\n"
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