-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathisPhoneNumberRE.py
More file actions
58 lines (45 loc) · 2.22 KB
/
isPhoneNumberRE.py
File metadata and controls
58 lines (45 loc) · 2.22 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import re
phoneNumberRegex=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') #regular expression compiles
mobject=phoneNumberRegex.search('My number is 415-654-9874') #search() searches for the given re in the entire string
print('Phone number found: '+mobject.group()) #group(0 gives the matched string
phnregex=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') #first parenthesis is for first group(1) and second parnhesis for second
mob=phnregex.search('My number is 415-654-9874')
print('Area :'+mob.group(1)+'\t Main Phone number :'+mob.group(2))
'''since mob.groups returns tuple we can use as'''
a,b=mob.groups()
print(a,b)
batregex=re.compile(r'Bat(man|mobile|copter)') #re for batman,batmobile,batcopter(several alternative patterns)
mobj=batregex.search('Batmobile lost a wheel')
print(mobj.group())
print('--optional matching wit question mark--')
optional=re.compile(r'Bat(wo)?man')
vs=optional.search('The adventures of Batman')
print(vs.group())
vd=optional.search('The adventures of Batwoman')
print(vd.group())
print('--matching zero or more with the star--')
opt=re.compile(r'Bat(wo)*man')
vde=opt.search('The adventures of Batman')
print(vde.group())
vdw=opt.search('The adventures of Batwowowowoman')
print(vdw.group())
print('--matching 1 or more with the plus--')
plus=re.compile(r'Bat(wo)+man')
pl=plus.search('The adventures of Batwowoman')
print(pl.group())
print('--matching specific repetition with curly bracket--')
repet=re.compile(r'(Ha){3}')
robj=repet.search('HaHaHaHa')
print(robj.group())
print('--Greedy and Nongreedy matching--')
print('--Greedy--it matches the longest one')
greedy=re.compile(r'(Ha){3,4}') #it matches the longest one i.e. 4
grob=greedy.search('Dont do HaHaHaHaHa')
print(grob.group())
print('--NonGreedy--it matches the shortest one')
nongreedy=re.compile(r'(Ha){2,5}?') #it is followed by question mark
ngd=nongreedy.search('Dont do HaHaHaHaHa')
print(ngd.group())
print('---findall() method---') #it returns all the matched string while search() method returns only the first match in whole string
phn=re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
print(phn.findall('Cell: 415-541-4562 work: 789-954-6547')) #it does not return object