import re def normalize_number(phone_number): pattern = re.compile("([\+]*[1][ -])*(\(*[2-9][0-9][0-9]\)*)[- .]([0-9][0-9][0-9])[ -.]([2-9][0-8][0-9][0-9])") if not pattern.match(phone_number): raise ValueError("Ungültige Telefonnummer") normalized_number = re.sub("\+*\(*\)*", "", phone_number) normalized_number = re.sub("[ .]", "-", normalized_number) if normalized_number[0] != str(1): normalized_number = "1-" + normalized_number return normalized_number