import re
regex = re.compile(r"^\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$", flags=re.MULTILINE)
test_str = ("1-718-444-1122\n"
"718-444-1122\n"
"(718)-444-1122\n"
"17184441122\n"
"7184441122\n"
"718.444.1122\n"
"1718.444.1122\n"
"1-123-456-7890\n"
"1 123-456-7890\n"
"1 (123) 456-7890\n"
"1 123 456 7890\n"
"1.123.456.7890\n"
"+91 (123) 456-7890\n"
"18005551234\n"
"1 800 555 1234\n"
"+1 800 555-1234\n"
"+86 800 555 1234\n"
"1-800-555-1234\n"
"1 (800) 555-1234\n"
"(800)555-1234\n"
"(800) 555-1234\n"
"(800)5551234\n"
"800-555-1234\n"
"800.555.1234\n"
"18001234567\n"
"1 800 123 4567\n"
"1-800-123-4567\n"
"+18001234567\n"
"+1 800 123 4567\n"
"+1 (800) 123 4567\n"
"1(800)1234567\n"
"+1800 1234567\n"
"1.8001234567\n"
"1.800.123.4567\n"
"+1 (800) 123-4567\n"
"18001234567\n"
"1 800 123 4567\n"
"+1 800 123-4567\n"
"+86 800 123 4567\n"
"1-800-123-4567\n"
"1 (800) 123-4567\n"
"(800)123-4567\n"
"(800) 123-4567\n"
"(800)1234567\n"
"800-123-4567\n"
"800.123.4567\n"
"1231231231\n"
"123-1231231\n"
"123123-1231\n"
"123-123 1231\n"
"123 123-1231\n"
"123-123-1231\n"
"(123)123-1231\n"
"(123)123 1231\n"
"(123) 123-1231\n"
"(123) 123 1231\n"
"+99 1234567890\n"
"+991234567890\n"
"(555) 444-6789\n"
"555-444-6789\n"
"555.444.6789\n"
"555 444 6789\n"
"18005551234\n"
"1 800 555 1234\n"
"+1 800 555-1234\n"
"+86 800 555 1234\n"
"1-800-555-1234\n"
"1.800.555.1234\n"
"+1.800.555.1234\n"
"1 (800) 555-1234\n"
"(800)555-1234\n"
"(800) 555-1234\n"
"(800)5551234\n"
"800-555-1234\n"
"800.555.1234\n"
"(003) 555-1212\n"
"(103) 555-1212\n"
"(911) 555-1212\n"
"18005551234\n"
"1 800 555 1234\n"
"+86 800-555-1234\n"
"1 (800) 555-1234")
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