def isPhoneNumber(text):
if len(text) != 12:
return False
for i in range(0, 3):
if not text[i].isdecimal():
return False
if text[3] != '-':
return False
for i in range(4, 7):
if not text[i].isdecimal():
return False
if text[7] != '-':
return False
for i in range(8, 12):
if not text[i].isdecimal():
return False
return True
def answer(text):
if isPhoneNumber(text):
print(text + ' est un numéro américain')
else:
print(text + " n'est pas un numéro américain")
answer('415-555-4242')
answer('04-91-50-50-97')
import re
# version américaine
americanPhoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = americanPhoneNumRegex.search('My american number is 415-555-4242.') # match object
if mo:
print('Numéro de téléphone américain')
print('American phone number found: ' + mo.group())
# version française
frenchPhoneNumRegex = re.compile(r'\d\d-\d\d-\d\d-\d\d-\d\d')
mo2 = frenchPhoneNumRegex.search('My american number is 01-77-50-59-42.') # match object
if mo2:
print('Numéro de téléphone français')
print('Numéro de téléphone français trouvé : ' + mo2.group())
Nous avons vu dans ce cours :