import re
regex = re.compile(r"^I\([^()]*\){(?:\R(?!}|.*n=Marc).*)*\R.*\bn=Marc\b.*(?:\R(?!}).*)*\R}$", flags=re.MULTILINE)
test_str = ("I(0,123...789){\n"
"A(0,567...999){.......n=Marc.....}\n"
"B(2,655...265){..................}\n"
"C(3,993...333){..................}\n"
"M(8,635...254){.................;}\n"
"}\n"
"O(0,345...789){\n"
"A(0,567...999){.......n=Marc.....}\n"
"B(2,876...775){..................}\n"
"C(3,993...549){..................}\n"
"M(8,354...987){.................;}\n"
"}\n"
"I(0,987...764){\n"
"A(0,567...999){.......n=Marc.....}\n"
"B(2,543...265){..................}\n"
"C(7,998...933){..................}\n"
"M(8,645...284){.................;}\n"
"}\n"
"B(0,123...789){\n"
".......\n"
"}\n"
"I(0,987...764){\n"
"A(0,567...999){.......n=John.....}\n"
"B(2,543...265){..................}\n"
"C(7,998...933){..................}\n"
"M(8,645...284){.................;}\n"
"}")
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