22 lines
697 B
Python
22 lines
697 B
Python
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
|