Last active
July 25, 2016 11:54
-
-
Save moneyismoney/30c3fad122e82931d3406e4fa140ca3f to your computer and use it in GitHub Desktop.
Test of regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This script creates dictionary, where we have mobile operator and mobile code | |
import re | |
#country_code_ukraine = '+380' | |
string_may_be = '38066-44-89-555aaa' | |
def parse_phone_for_provider(string_may_be): | |
final_result = dict(carier='', code='', number='') | |
if (string_may_be is None): | |
print ('There is no value in input variavle. Exception') | |
else: | |
print 'We will check it ',string_may_be | |
#1. Clean all symbols except numbers | |
print '\n String with only digits: ' | |
clean_string = re.sub('\D','',string_may_be) | |
print clean_string | |
#2. Create dictionary of known mobile providers (from Wiki) | |
mob = dict() | |
mob['50']='Vodafone' | |
mob['95']='Vodafone' | |
mob['66']='Vodafone' | |
mob['99']='Vodafone' | |
mob['63']='lifecell' | |
mob['73']='lifecell' | |
mob['93']='lifecell' | |
mob['68']='Kyivstar' | |
mob['67']='Kyivstar' | |
mob['96']='Kyivstar' | |
mob['97']='Kyivstar' | |
mob['98']='Kyivstar' | |
mob['91']='3mob (Ukraine)' | |
mob['92']='PEOPLEnet' | |
mob['68']='Intertelecom' | |
# 2a | |
if len(clean_string) == 12: #[380-67-1234567] Something+2+7 | |
result = re.match(r'(?P<code>\d{3})(?P<carier>\d{2})(?P<number>\d{7})', clean_string) | |
elif len(clean_string) == 11: #[80-67-1234567] Something+2+7 | |
result = re.match(r'(?P<code>\d{2})(?P<carier>\d{2})(?P<number>\d{7})', clean_string) | |
elif len(clean_string) == 10: #[0-67-1234567] Something+2+7 | |
result = re.match(r'(?P<code>\d{1})(?P<carier>\d{2})(?P<number>\d{7})', clean_string) | |
else: | |
return False | |
final_result = result.groupdict() | |
final_result['carier'] = mob.get(final_result['carier']) | |
#3. Return | |
return final_result; | |
if __name__ == '__main__': | |
print parse_phone_for_provider(string_may_be) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment