import re
regex = re.compile(r"\{\{ *(ab|cd|ef|gh) *\}\} *\{\{ *(ab|cd|ef|gh) *\}\} *(\{\{ *(ab|cd|ef|gh) *\}\})* *(\{\{ *(ab|cd|ef|gh) *\}\})* *(\{\{ *(ab|cd|ef|gh) *\}\})* *(\{\{ *(ab|cd|ef|gh) *\}\})*", flags=re.MULTILINE)
test_str = ("{{ab}}{{ab}}{{ab}}{{ab}}{{ab}}{{ab}}\n"
"{{ab}} {{ab}} {{ab}} {{ab}} {{ab}} {{ab}}\n"
"machintruc {{ab}} {{cd}}\n"
"{{ab}} {{cd}} {{ef}} [https://truc.org truc]\n"
"{{ab}}{{ cd }}{{ ef }} {{ gh }} {{ gh }}{{lien web|url=tr.uc}}\n"
"{{mr}} Trucmuche\n")
subst = "{{mul|$1|$2|$4|$6|$8|$10}} "
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