pr3-s2/s2_a4_a.py

22 lines
697 B
Python
Raw Normal View History

2023-01-09 17:41:29 +01:00
import re
def parse_number(number):
pattern = r'^(?:\+?1[- ])?(\(\d{3}\)|\d{3})[-. ](\d{3})[-. ](\d{4})$'
if not re.fullmatch(pattern, number):
raise ValueError("Ungültige Telefonnummer")
segments = re.search(pattern, number)
ortsvorwahl = segments.group(1).replace("(", "").replace(")", "")
amtskennziffer = segments.group(2)
vorwahl = segments.group(3)
if ortsvorwahl[0] <= '1' and ortsvorwahl[0] >= '0':
raise ValueError("Ungültige Telefonnummer")
if (vorwahl[0] <= '1' and vorwahl[0] >= '0') or vorwahl[1] == '9':
raise ValueError("Ungültige Telefonnummer")
return "1-" + ortsvorwahl + "-" + amtskennziffer + "-" + vorwahl