import re
regex = re.compile(r"\{\{\s*Template1\s*(.*?)\n\}\}", flags=re.IGNORECASE | re.DOTALL)
test_str = ("...\n"
"abas asdn asf asfs af\n"
"{{Template1\n"
"|a = Name surname\n"
"|b = jhsdf sdf\n"
"|c = {{Template2}}\n"
"|d = \n"
"|e = [[f]] and [[g]]\n"
"|h = asd asdasfgasgasg asgas jygh trdx dftf xcth\n"
"|i = 73\n"
"|j = {{Template2|abc|123}}\n"
"|j = {{Template3|aa=kkk|bb={{Template4|cc=uu}}}}\n"
"}}\n\n"
"asd wetd gdsgwew g\n\n"
"{{OtherTemplate\n"
"|sdf = 213\n"
"}}\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