# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^\+\d{1,4}(( |\(| \(|-)?\d{1,4}( |\)|\) |-)?)?(\d{2} )?\d{2,4}[- ]?\d{2,3}[- ]?\d{0,2}$"
test_str = ("+22-22-222-2222\n"
"+777(777)777-777\n"
"+666-66-666-6666\n"
"+9(999)999-9999\n"
"+444-444-444\n"
"+555(555)555-555\n"
"+0(000)000-0000\n"
"+1(111)111-1111\n"
"+11(111)111-1111\n"
"+111-11-111-111\n"
"+666-666-6666\n"
"+99 999 999 999\n"
"+77(777)777-7777\n"
"+999-99-999-99-99\n"
"+8(888)888-8888\n"
"+333-3333-3333\n"
"+000-00-000-000\n"
"+7(777)777-7777\n"
"+111(11)111-11-11\n"
"+55 555 55 55 55\n"
"+222-222-2222\n"
"+888-88-88-8888\n"
"+4(444)444-4444\n"
"+333-3-333-333\n"
"+555-5-555-5555\n"
"+555-55-5555\n"
"+666-66-666-666\n"
"+55-55-5555-5555\n"
"+999-999-9999\n"
"+5(555)555-5555\n"
"+666-666-6666\n"
"+111(111)111-111\n"
"+666-66-66-6666\n"
"+666-66-66-6666\n"
"+666-66-666-666\n"
"+000-0000-0000\n"
"+2 (222) 222-2222\n"
"+111(111)11-11\n"
"+999-999-9999\n"
"+0(000)000-0000\n"
"+555-55-55-5555\n"
"+444-44-44-44-44\n"
"+88-8-8888-8888\n"
"+11 11-11111111\n"
"+77(777)777-7777\n"
"+111-11-11111\n"
"+666(666)666-666\n"
"+999-99-999-9999\n"
"+777-77-777\n"
"+111 1111-1111\n"
"+666-66-666-666\n"
"+555-55-555-555\n"
"+33-3-333-3333\n"
"+666-666-6666\n"
"+222-22-222-222\n"
"+666(666)666-666\n"
"+55 55 55 55 55\n"
"+888-88-88-88-88\n"
"+5(555)555-5555\n"
"+7(777)777-7777\n"
"+111-1-111-1111\n"
"+66(666)666-6666\n"
"+111 1111-1111\n"
"+666-66-666-6666\n"
"+333-3-333-333\n"
"+000-000-0000\n"
"+222-22-222-2222\n"
"+666-66666\n"
"+777-777-777\n"
"+666-66-66666\n"
"+222 22 222 22 22\n"
"+33 3 33 33 33 33\n"
"+333-33333-3333\n"
"+777-77-77-77\n"
"+999-9-99-99-99\n"
"+555(555)55-55\n"
"+666(666)666-666\n"
"+55 555 5555555\n"
"+000(000)000-000\n"
"+000-000-00000\n"
"+55(555)555-5555\n"
"+444-44-44-44\n"
"+5(555)555-5555\n"
"+5(555)555-5555\n"
"+111 1111-1111\n"
"+444-44-444-444\n"
"+555-5-555555\n"
"+222-222-2222\n"
"+333 3333-3333\n"
"+000-0000-0000\n"
"+666 6666 6666\n"
"+88(888)888-888\n"
"+222 222 2222\n"
"+22 22222-22222\n"
"+77-77-777-77\n"
"+11(111)111-1111\n"
"+333(333)333-3333\n"
"+111 11 1111111\n"
"+999-9-999-9999\n"
"+99 999 999999\n"
"+7(777)777-7777\n"
"+99 999 99 9999\n"
"+000-0-0000-0000\n"
"+2 222 222-22-22\n"
"+444-444-444444\n"
"+222-22-222\n"
"+777-7777-7777\n"
"+111(111)111-111\n"
"+000-00-000-000\n"
"+444-44-444-444\n"
"+999-9-999-999\n"
"+111-1-111-1111\n"
"+888-88-888-888\n"
"+666-66-666-666\n"
"+333(333)333-3333\n"
"+666(666)66-666\n"
"+222(222)222-222\n"
"+111-1111-1111\n"
"+999-99-999-999\n"
"+222-22-22-22222\n"
"+555-5-5555-5555\n"
"+33 33-3333-3333\n"
"+000-000-0000\n"
"+222-22-22-2222\n"
"+333-3333-3333\n"
"+555-555-5555\n"
"+555(555)55-55-55\n"
"+111-11-11-1111\n"
"+777-777-7777\n"
"+11-11-11-1111\n"
"+999-999-9999\n"
"+666-6666-6666\n"
"+444-44-444-444\n"
"+111-11-11-1111\n"
"+999-99-999-999\n"
"+0(000)000-0000\n"
"+333-33-3333-333\n"
"+333-33-333-333\n"
"+33-333-333\n"
"+888-88-888-8888\n"
"+333-333-3333\n"
"+777-77-777-777\n"
"+44 44 44444444\n"
"+888-88-8888\n"
"+44 444-444-4444\n"
"+777-7777-7777\n"
"+444-44-44-4444\n"
"+222-22-222-22\n"
"+222-2222\n"
"+333-333-333\n"
"+222-222-222\n"
"+2(222)222-2222\n"
"+66 666 66 666\n"
"+666-66-666-666\n"
"+44 444-4444444\n"
"+666-666-6666\n"
"+555-55-555-5555\n"
"+000-000-0000\n"
"+666(666)66-666\n"
"+222(222)222-222\n"
"+44(444)444-444\n"
"+11 111 1111\n"
"+88 888-888-888\n"
"+222-22-222-2222\n"
"+111-1111-1111\n"
"+888-88888-8888\n"
"+99-99-999-9999\n"
"+2 222 222-22-22\n"
"+999(999)999-999\n"
"+4(444)444-4444\n"
"+6(666)666-6666\n"
"+2(222)222-2222\n"
"+999-99-9999\n"
"+111-1111-111111\n"
"+777-77-77777\n"
"+222-2-222-2222\n"
"+222-22-222-2222\n"
"+555-55-555-5555\n"
"+222-2-222-222\n"
"+222-22-222222\n"
"+11 1111-1111\n"
"+2(222)222-2222\n"
"+111(111)111-111\n"
"+777-77-777-777\n"
"+777-77777\n"
"+777-7-777-777\n"
"+99-99-999-9999\n"
"+44-44-444-4444\n"
"+222-22-222-2222\n"
"+77 777 777 777\n"
"+55-55-555-5555\n"
"+555-55-555-5555\n"
"+444-444-444\n"
"+999-99-99-9999\n"
"+77 77 777 77 77\n"
"+55 55 555 55 55\n"
"+999-99-9999-999\n"
"+999-9999-9999\n"
"+888-88-888-8888\n"
"+333-33-333-3333\n"
"+44-44-444-444\n"
"+222-222-2222\n"
"+777-77-777-777\n"
"+777-7777\n"
"+333-33333\n"
"+5(555)555-5555\n"
"+444-44-444-444\n"
"+44 444 444 44 44\n"
"+777-7-777-7777\n"
"+1(111)111-1111\n"
"+222-22222\n"
"+2(222)222-2222\n"
"+111(111)111-111\n"
"+000(00)000-00-00\n"
"+000-0-000-0000\n"
"+77 7777 777777\n"
"+2 (222) 222-2222\n"
"+666-6-666-66-66\n"
"+666-66-666-6666\n"
"+111-11111\n"
"+11 11 1111 1111\n"
"+00(000)000-0000\n"
"+33-33-3333-333\n"
"+444-44-4444\n"
"+444-4-444-444\n"
"+888-88-888-8888\n"
"+555-5-555555")
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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