import re
regex = re.compile(r"(.*)\r?\n(.*)\r?\n(.*)(?:\r?\n|$)", flags=re.MULTILINE)
test_str = ("1000\n"
"AAAAAAAAAA\n"
"100,000.00\n"
"2000\n"
"BBBBBBBBBB\n"
"200,000.00\n"
"3000\n"
"CCCCCCCCCC\n"
"300,000.00\n"
"4000\n"
"DDDDDDDDDD\n"
"400,000.00")
subst = "$1|$2|$3\\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