import re
regex = re.compile(r"""
^(\+?\d[\h(-]{0,3})? # Remove country code, "+", " ", and "("
(\d{3}) # Area code
[\h)-.]{0,3} # Remove "-", " ", ".", and ")"
(\d{3}) # Three digits
[\h-.]{0,3} # Remove "-", " ", and "."
(\d{4}) # Four digits
""", flags=re.MULTILINE | re.VERBOSE)
test_str = ("+1 123 111 1111\n"
"+1-111-111-1234\n"
"+1 (111) 111-1111\n"
"1 111 111 1111\n"
"1-111-111-1111\n"
"1-(111) 111-4567\n"
"1111111111\n"
"111.111.8765\n"
"111-345-1111")
subst = "$2.$3.$4"
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