# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(([+]{1}[1-9]{1}[0-9]{0,2}[ ]{1}([1-9]{1}[0-9]{1,4}){1}[ ]{1}([1-9]{1}[0-9]{2,6}){1}([ -][0-9]{1,5})?)|([0]{1}[1-9]{1}[0-9]{1,4}[ ]{1}[0-9]{1,8}([ -][0-9]{1,8})?)?)"
test_str = ("06429 1111\n"
"06901 306180\n"
"06429 231\n"
"0800 3301000\n"
"0179 1111111\n"
"0873 376461\n"
"03748 37682358\n"
"05444 347687-350\n"
"0764 812632-41\n"
"0180 2 12334\n"
"0800 5 23234213\n"
"+49 6429 1111\n"
"+49 39857 2530\n"
"+55 11 2666-0054\n"
"+300 11 2666-0054\n"
"+49 641 20106 0\n"
"+49 641 20106\n"
"+49 30 3432622-113\n\n"
"------\n\n"
"+300 11 0000-0000\n"
"(06442) 3933023\n"
"(02852) 5996-0\n"
"(042) 1818 87 9919\n"
"06442 / 3893023\n"
"06442 / 38 93 02 3\n"
"06442/3839023\n"
"042/ 88 17 890 0\n"
"+49 221 - 542194 79\n"
"+49 (221) - 542944 79\n"
"0 52 22 - 9 50 93 10\n"
"+49(0)121-79536 - 77\n"
"+49(0)2221-39938-113\n"
"+49 (0) 1739 906-44\n"
"+49 (173) 1799 806-44\n"
"0173173990644\n"
"0214154914479\n"
"01517953677\n"
"+491517953677\n"
"015777953677\n"
"02162 - 54 91 44 79\n"
"(02162) 54 91 44 79\n"
"saddsadasdasd\n"
"asdasd\n"
"asdasd asdasd asd\n"
"asdasd\n"
"kjn asohas asdoiasd\n"
"23434 234 234 23\n"
"323\n"
"23434 234----234\n"
"///// ----\n"
"// id8834 3493934 //")
matches = re.finditer(regex, test_str, re.MULTILINE)
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