-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisphonenumber.py
More file actions
35 lines (28 loc) · 755 Bytes
/
Copy pathisphonenumber.py
File metadata and controls
35 lines (28 loc) · 755 Bytes
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
import re
def isphonenumber(num):
if len(num) != 12:
return False
for i in range(len(num)):
if i == 3 or i == 7:
if num[i] != '-':
return False
elif num[i].isdigit() == False:
return False
return True
def checkphone(num):
phnumpattern = re.compile(r'\d{3}-\d{3}-\d{4}')
if phnumpattern.match(num):
return True
else:
return False
ph_num = input("Enter a phone number:\n")
print("Without using regular expression:\n")
if isphonenumber(ph_num):
print("Valid phone number")
else:
print("Invalid phone number")
print("Using regular expression:")
if checkphone(ph_num):
print("Valid phone number")
else:
print("Invalid phone number")