import re
pattern=re.compile('python')
print(type(pattern))
Output:
<class 're.Pattern'>
#Python program to find the pattern ab in a target string ababbaba and find the no. of occu
import re
count=0
pattern=re.compile('ab')
matcher=pattern.finditer('ababbaba')
for m in matcher:
count+=1
#print('match is available at start index',m.start())
print('start:{},end:{},group:{}'.format(m.start(),m.end(),m.group()))
print('the no. of occurences is:',count)
Output:
start:0,end:2,group:ab
start:2,end:4,group:ab
start:5,end:7,group:ab
the no. of occurences is: 3
#finditer(): import re
matcher=re.finditer('[abc]','a7b@k9z')
for m in matcher:
print(m.start(),'....',m.group())
Output:
0 .... a
2 .... b
#match()
import re
s=input('enter pattern to check')
m=re.match(s,'abcdefgh')
if m!=None:
print('match is available at the begining of the string')
print('start index:{} and end index:{}'.format(m.start(),m.end()))
else:
print('match is not available at the begining of the string')
Output:
enter pattern to checkabc
match is available at the begining of the string
start index:0 and end index:3
#fullmatch()
import re
s=input('enter pattern to check')
m=re.fullmatch(s,'abcdefgh')
if m!=None:
print('full string matched')
else:
print('full string not matched')
Output:
enter pattern to checkabcdefgh
full string matched
#search()
import re
s=input('enter pattern to check') m=re.search(s,'abaabaaab')
if m!=None:
print('match is available')
print('First occurence with start index:{} and end index:{}'.format(m.start(),m.end()))
else:
print('match is not available')
Output:
enter pattern to checkaaa
match is available
First occurence with start index:5 and end index:8
#findall():
import re
l=re.findall('[0-9]','a7b9k6z')
print(l)
Output:
['7', '9', '6']
#split():
import re
l=re.split(' ','Silan Software')
for k in l:
print(k)
Output:
Silan
Software
#Regular expression to represent 10 digits mobile number
import re
s=input('enter mobile number to validate')
m=re.fullmatch('[6-9]\d{9}',s)
if m!=None:
print(s,'is valid mobile number')
else:
print(s,'is not valid mobile number')
Output:
enter mobile number to validate9439202111
9439202111 is valid mobile number
#Regular expression to check valid or invalid gmail id
import re
s=input('enter mail id:')
m=re.fullmatch('\w[a-zA-Z0-9_.]*@gmail[.]com',s)
if m!=None:
print('valid gmail id')
else:
print('invalid gmail id')
Output:
enter mail id:trilochan4u@gmail.com
valid gmail id
#Regular expression to check valid or invalid mail id
import re
s=input('enter mail id:')
m=re.fullmatch('\w[a-zA-Z0-9_.]*@[a-z0-9]+[.][a-z]+',s)
if m!=None:
print('valid mail id')
else:
print('invalid mail id')
Output:
enter mail id:govind_silan@3s.net
valid mail id
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc