import re
regex = re.compile(r"^([a-z]+-[a-z]+[0-9]+-[a-z]+)$|^([a-z]+[0-9]+-[a-z]+[0-9]+-[a-z])$|^([a-z]+-[a-z]+-[a-z])$", flags=re.MULTILINE | re.IGNORECASE)
test_str = ("aaa-b3-c\n"
"a3-b6-c\n"
"aaaa-bb-c\n"
"aaa-b3-c3\n"
"a3-1b6-c\n"
"aaaa-bb-c7")
subst = "\\1\\2\\3"
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