import re
regex = re.compile(r"^(\w+) +\w+ +(?:\[[^\]]+\] *)?(\S.*?)(?: +(\([^)]+\)))?$", flags=re.MULTILINE)
test_str = ("0000 10 Text(\"TOTAL,SOME RANDOM TEXT\") (1122aabb)\n"
"0010 5 D==1122aabb (1122aabb)\n"
"0015 17 Text(\"AND,SOME,MORE\") (00000001)\n"
"002c 5 D==1 (1)\n"
"0031 1 !D (ccdd3344)\n"
"0032 5 D==ccdd3344 (ccdd3344)\n"
"0037 2 !1 (1)\n"
"0039 0 [AAAA] Fff\n"
"0039 1 [BBBB] Aaa\n"
"003a 6 N(05, eeff5566) (eeff5566)\n"
"0040 1 Qq\n"
"0041 2 $ab ([String]:\"Unknown\")\n"
"0043 f Call A/SomeFunc-X\n"
"0052 1 cd")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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