import re
regex = re.compile(r"^(HORIZON-\S+-[A-Z]\d-\d{2}\S*)(?:\r?\n(?!Opening\b).*)*\r?\nOpening: (\d{2} [A-Z][a-z]{2} \d{4})\r?\nDeadline\(s\): (\d{2} [A-Z][a-z]{2} \d{4}.*)", flags=re.MULTILINE)
test_str = ("\n"
"HORIZON-CL5-2021-D1-01\n"
"Conditions for the Call\n"
"Indicative budget(s) \n"
"Topics Type of Action Budgets (EUR million) Expected EU contribution per project (EUR million) Number of projects expected to be funded\n"
" 2021 \n"
"Opening: 15 Apr 2021\n"
"Deadline(s): 07 Sep 2021\n\n"
"HORIZON-CL5-2022-D1-01-two-stage\n"
"Conditions for the Call\n"
"Indicative budget(s) \n"
"Topics Type of Action Budgets (EUR million) Expected EU contribution per project (EUR million) Number of projects expected to be funded\n"
" 2022 \n"
"Opening: 04 Nov 2021\n"
"Deadline(s): 15 Feb 2022 (First Stage), 07 Sep 2022 (Second Stage)\n\n\n"
"HORIZON-CL5-2021-D2-01\n"
"Conditions for the Call\n"
"Indicative budget(s) \n"
"Topics Type of Action Budgets (EUR million) Expected EU contribution per project (EUR million) Number of projects expected to be funded\n"
" 2021 2022 \n"
"Opening: 15 Apr 2021\n"
"Deadline(s): 19 Oct 2021")
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