automata-labs/TaskC37/solution.py

15 lines
489 B
Python
Raw Permalink Normal View History

2024-12-18 18:03:49 +01:00
import re
import sys
def extract_area_code(line: str) -> str:
# While this regex seems complicated, it essentially enforces either of two possible formats
# Returning the area code captured in group(1)
p = re.compile(r'^[+0](\d{2})\s(?:\d-\d{3}-\d{3}|\d{3}-\d{2}-\d{2})$')
m = p.match(line)
return m.group(1) if m else "<NONE>"
if __name__ == "__main__":
for line in sys.stdin:
stripped_line = line.strip()
print(extract_area_code(stripped_line))