-
Notifications
You must be signed in to change notification settings - Fork 0
/
re_phone.py
33 lines (22 loc) · 840 Bytes
/
re_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
import re
def extract_phone(string):
phone_regex = re.compile(r"\b\d{3} \d{3}-\d{4}\b")
match = phone_regex.search(string)
if match:
return match.group()
def extract_all_phone_numbers(string):
phone_regex = re.compile(r"\b\d{3} \d{3}-\d{4}\b")
return phone_regex.findall(string)
def is_valid_phone(string):
phone_regex = re.compile(r"^\d{3} \d{3}-\d{4}$")
match = phone_regex.search(string)
if match:
return True
return False
print(extract_phone("my number is 432 567-4432"))
print(extract_phone("my number is 4332 567-4432"))
print(extract_all_phone_numbers("my number is 432 567-4432 or call me at 443 882-1133"))
print(is_valid_phone("number 432 567-4432"))
print(is_valid_phone("432 567-4432"))
print(is_valid_phone("432 567-4432x"))
print(is_valid_phone("432 567-44323"))