Links

Find Phone Number in string

Tags: #python #string #number #naas #operations #snippet
Author: Anas Tazir​
This notebook find phone number in string.

Input

Setup Variables

# Input
sentence = """My number is +33606060606 """
​
# Ouptut
phone_number = ""

Model

Find Phone Number

def find_phone_number(string):
if len(string) == 10 and string.isdecimal(): # if string is of type XXXXXXXXXX
return True
if len(string) == 12 and string.startswith("+") and string[1:].isdecimal(): # if string is of type XXXXXXXXXX
return True
if len(string) != 12: # if string is of type XXX-XXX-XXXX
return False
if string.isdecimal():
return True
for i in range(0, 3):
if not string[i].isdecimal():
return False
if string[3] != '-':
return False
for i in range(4, 7):
if not string[i].isdecimal():
return False
if string[7] != '-':
return False
for i in range(8, 12):
if not string[i].isdecimal():
return False
return True
for i in range(len(sentence)):
split_12 = sentence[i:i+12]
split_10 = sentence[i:i+10]
if find_phone_number(split_10):
phone_number = split_10
break
elif find_phone_number(split_12):
phone_number = split_12
break

Output

Display result

if not phone_number:
print("Not phone number was found.")
else:
print("Phone number present in the string is :", phone_number)