fork download
  1. import re
  2.  
  3. def format_phone_number(phone_number):
  4. pn = re.sub(r'[^\d]', '', phone_number)
  5. pn = re.sub(r'(\d{3})(?!\d{1}$|$)', r'\1-', pn)
  6. pn = re.sub(r'(\d{2})(\d{2})', r'\1-\2', pn)
  7. return pn
  8.  
  9. print(format_phone_number("12"))
  10. print(format_phone_number("123"))
  11. print(format_phone_number("1234"))
  12. print(format_phone_number("12345"))
  13. print(format_phone_number("123456"))
  14. print(format_phone_number("1234567"))
  15. print(format_phone_number("12345678"))
  16. print(format_phone_number("123456789"))
  17. print(format_phone_number("1234567890"))
  18. print(format_phone_number("3-234 234 23 523052 342-234"))
  19. print(format_phone_number("5 34 334 23-4-234 52 345 2-34 52345"))
  20. print(format_phone_number("534 93495 352382 "))
Success #stdin #stdout 0.02s 27720KB
stdin
Standard input is empty
stdout
12
123
12-34
123-45
123-456
123-45-67
123-456-78
123-456-789
123-456-78-90
323-423-423-523-052-342-234
534-334-234-234-523-452-345-23-45
534-934-953-523-82