-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphone.py
46 lines (36 loc) · 1.3 KB
/
phone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import re
# Returns first instance of phone number pattern:
def extract_phone(input):
phone_regex = re.compile(r'\b\d{3} \d{3}-\d{4}\b')
match = phone_regex.search(input)
if match:
return match.group()
return None
# Returns all instances of phone number pattern in a list
def extract_all_phones(input):
phone_regex = re.compile(r'\b\d{3} \d{3}-\d{4}\b')
return phone_regex.findall(input)
# One way of checking if entire string is valid phone number:
# def is_valid_phone(input):
# phone_regex = re.compile(r'^\d{3} \d{3}-\d{4}$')
# match = phone_regex.search(input)
# if match:
# return True
# return False
# Another way of doing the same thing, using the fullmatch method
def is_valid_phone(input):
phone_regex = re.compile(r'\d{3} \d{3}-\d{4}')
match = phone_regex.fullmatch(input)
if match:
return True
return False
# Calling our functions a bunch of times...
print(extract_phone("my number is 432 567-8976"))
print(extract_phone("my number is 432 567-897622"))
print(extract_phone("432 567-8976 asdjhasd "))
print(extract_phone("432 567-8976"))
print(extract_all_phones("my number is 432 567-8976 or call me at 345 666-7899"))
print(extract_all_phones("my number is 432 56"))
print(is_valid_phone("432 567-8976"))
print(is_valid_phone("432 567-8976 ads"))
print(is_valid_phone("asd 432 567-8976 d"))