Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- test_numbers = ["+7-922-613-80-24","8-922-613-80-24","7 922 613 80 24","7-922-613-80-24", "9226138024", "89226138024"]
- class PhoneNumberException(Exception):
- def __init__(self, text):
- self.txt = text
- def return_number(match_obj):
- country = match_obj.group('country')
- country = '+7'
- return('{} ({}) {}-{}-{}'.format(country, match_obj.group('area'), match_obj.group('exch'), match_obj.group('number'), match_obj.group('number1')))
- 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})")
- for number in test_numbers:
- if re.match(r"((?:\+7|7|8)(?:[\s|-]\d{2,3}){4})", number):
- print(regexp.sub(return_number, number))
- else:
- raise PhoneNumberException("Not valid")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement