# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:\B\+ ?49|\b0)(?: *[(-]? *\d(?:[ \d]*\d)?)? *(?:[)-] *)?\d+ *(?:[/)-] *)?\d+ *(?:[/)-] *)?\d+(?: *- *\d+)?"
test_str = ("+49 39291 55-217\n"
"02102 7007064\n"
"0152 01680970\n"
"+49 39291 55-216\n"
"02102 3802 22\n"
"0800 333004 451-100\n"
"+49 221 9937 26950\n"
"02151-47974510\n"
"+49(0)6105 937 -539\n"
"0211/409 2268\n"
"+49(0)6105 937 -539\n"
"+49211/584-623\n"
"0211 58422 2012\n"
"+49 (9131) 7-35335\n"
"+49 521 9488 2470\n"
"+ 49-40-70 70 84 - 0\n"
"0211 17 95 99 04\n"
"02151-47974327\n"
"+49 203 28900 1121\n"
"0211 9449-2555\n"
"+49 (5 41) 9 98 -2268")
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