fork download
  1. import re
  2. phones = ['00-44 48 5555 8361', '34535 43 - 23', '023423 1 2-00',
  3. '345345345345', '(345)-345765 345 75', '8(913)345-341 2 3']
  4. def phones_parse(phone):
  5. regex = r'[ -]|[\)\(]*'
  6. phone = [re.sub(regex,'',x) for x in phone]
  7. newphones = []
  8. for i in phone:
  9. if len(i) % 3 == 2:
  10. k = len(i) - 2
  11. n, list = 0, []
  12. while True and n !=k:
  13. list.append(i[n:n+3])
  14. n +=3
  15. list.append(i[-2:])
  16. string = ('-').join(list)
  17. newphones.append(string)
  18. elif len(i) % 3 == 1:
  19. k = len(i) - 4
  20. n, list = 0, []
  21. while True and n != k:
  22. list.append(i[n:n + 3])
  23. n += 3
  24. list.append(i[-4:-2])
  25. list.append(i[-2:])
  26. string = ('-').join(list)
  27. newphones.append(string)
  28. else:
  29. k = len(i)
  30. n, list = 0, []
  31. while True and n != k:
  32. list.append(i[n:n + 3])
  33. n += 3
  34. string = ('-').join(list)
  35. newphones.append(string)
  36. return newphones
  37.  
  38. b = phones_parse(phones)
  39. print(b)
  40.  
Success #stdin #stdout 0.02s 27720KB
stdin
Standard input is empty
stdout
['004-448-555-583-61', '345-354-323', '023-423-12-00', '345-345-345-345', '345-345-765-345-75', '891-334-534-123']