PR3_Studienleistung_1_Prof_.../SL_2/s2_a4_a.py

17 lines
507 B
Python
Raw Normal View History

2023-01-13 18:49:35 +01:00
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