Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. import re
  2. test_numbers = ["+7-922-613-80-24","8-922-613-80-24","7 922 613 80 24","7-922-613-80-24", "9226138024", "89226138024"]
  3.  
  4. class PhoneNumberException(Exception):
  5. def __init__(self, text):
  6. self.txt = text
  7.  
  8. def return_number(match_obj):
  9. country = match_obj.group('country')
  10. country = '+7'
  11. return('{} ({}) {}-{}-{}'.format(country, match_obj.group('area'), match_obj.group('exch'), match_obj.group('number'), match_obj.group('number1')))
  12.  
  13. regexp = re.compile(r"(\+|)(?P<country>\d{1})([-|\s|(])(?P<area>\d{3})([-|\s|)])(?P<exch>\d{3})([-|\s|?])(?P<number>\d{2})([-|\s|?])(?P<number1>\d{2})")
  14. for number in test_numbers:
  15. if re.match(r"((?:\+7|7|8)(?:[\s|-]\d{2,3}){4})", number):
  16. print(regexp.sub(return_number, number))
  17. else:
  18. raise PhoneNumberException("Not valid")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement