41 RegularExpression
41 RegularExpression
Match():
match() will search for the “pattern” in the first word of target. If the
first word of target matched with given pattern then it will return “first
word” to the “Match” Object otherwise it will return “None”
Example :
import re
search():
search() will search for the “pattern” in the entire target.
If any matches are existed with given pattern then it will
return that “matched string” to the “Match” Object
otherwise it will return “None”
1|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
import re
findall()
findall( ) return list collection with matched string from target
whatever string are matched with given pattern . If None of the
match is exists then it will return an empty list []
import re
p=re.compile("m\w\w")
lst=p.findall("dad mom mam gun jug mad map nan")
print("Matched String : ",lst) #[mom,mam,mad,map]
'''
p=re.compile("m\w\w")
m=p.match("mom mad mam dad jug gun nan") '''
#re.match(pattern,target)
m=re.match("m\w\w","mom mad mam dad jug gun nan")
print("Match Object : ",m)
'''
p=re.compile("m\w\w")
m=p.search("mom mad mam dad jug gun nan") '''
2|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
#re.search(pattern,target)
m=re.search("m\w\w","dom mad mam dad jug gun nan")
print("Match Object : ",m)
'''
p=re.compile("m\w\w")
lst=p.findall("mom mad mam dad jug gun nan") '''
#Match Object
import re
print("Match : ",m.group())
print("Starting index pos of match : ",m.start())
print("Ending index pos+1 of match : ",m.end())
3|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Finditer() :
it will return callable_iterator Object . it is the collection of Match
Objects
import re
import time
for m in citr:
time.sleep(1)
print(m)
print("- "*40)
print("Match : ",m.group())
print("Starting index : ",m.start())
print("Ending index : ",m.end())
Output:
Type is : <class 'callable_iterator'>
citr Object : <callable_iterator object at 0x00000054500B34C0>
<re.Match object; span=(0, 3), match='mom'>
----------------------------------------
Match : mom
Starting index : 0
Ending index : 3
<re.Match object; span=(8, 11), match='mam'>
----------------------------------------
Match : mam
Starting index : 8
Ending index : 11
<re.Match object; span=(16, 19), match='mad'>
4|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
----------------------------------------
Match : mad
Starting index : 16
Ending index : 19
<re.Match object; span=(24, 27), match='mug'>
----------------------------------------
Match : mug
Starting index : 24
Ending index : 27
for m in citr:
time.sleep(1)
print("Match : ",m.group( ))
print("Index : ",m.start())
print("- "*30)
5|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example :
import re
import time
for m in citr:
time.sleep(1)
print("Match : ",m.group( ))
print("Index : ",m.start())
print("- "*30)
output:
Match : a
Index : 0
------------------------------
Match :
Index : 2
------------------------------
Match : $
Index : 3
------------------------------
Match : A
Index : 5
------------------------------
Match :
Index : 6
------------------------------
Match : *
Index : 7
------------------------------
Match : B
6|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Index : 8
------------------------------
Example:
import re
import time
citr=re.finditer("\w","a1 $7A *B")
for m in citr:
time.sleep(1)
print("Match : ",m.group( ))
print("Index : ",m.start())
print("- "*30)
Example :
import re
import time
citr=re.finditer(".","a1 $7A *B")
for m in citr:
time.sleep(1)
print("Match : ",m.group( ),"Index : ",m.start())
Output:
Match : a Index : 0
Match : 1 Index : 1
Match : Index : 2
Match : $ Index : 3
Match : 7 Index : 4
Match : A Index : 5
Match : Index : 6
Match : * Index : 7
Match : B Index : 8
7|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Note:
Whenever predefine patterns fail to fulfill requirements then
we have to define our own patterns called
“User defined” Pattern
[abc] either a or b or c
[aeiou] either a or e or i or o or u
[a-z] any lower case letters
[A-Z] any upper case letters
[a-zA-Z] any upper case or lower case letters
[^a-zA-Z] Except alphabets
[589] either 5 or 8 or 9
[5-9] from 5 to 9
[0-9] any digits or \d
[a-zA-Z0-9] Alpha and Numerical or \w
[^a-zA-Z0-9] Except Alpha and numerical or \W
Example:
import re
import time
citr=re.finditer("[a-z]","a1 $7A *BcD 9")
for m in citr:
time.sleep(1)
print("Match : ",m.group()," index : ",m.start())
8|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example 2:
import re
import time
output:
Match : a index : 0
Match : $ index : 3
Match : c index : 9
Match : D index : 10
Quantifiers :
No.of.occ of match
9|Page
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example:
import re
import time
Output:
Match : a index : 0
Match : aa index : 2
Match : aaa index : 6
Match : aaaa index : 11
Example:
import re
import time
citr=re.finditer("a*","a aab aaab aaaab")
for m in citr:
time.sleep(1)
print("Match : ",m.group(),"index : ",m.start())
output:
Match : a index : 0
Match : index : 1
Match : aa index : 2
Match : index : 4
Match : index : 5
10 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Match : aaa index : 6
Match : index : 9
Match : index : 10
Match : aaaa index : 11
Match : index : 15
Match : index : 16
App-2:
[6-9][0-9]+ //invalid input 68 //valid
Input 78888888888888888888 //valid
[6-9][0-9]* //invalid
Input 9 //valid
Input 9898 //valid
Input 98888888888888888888888888888888 //valid
11 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
App2: Patter for validate mobile number
Need to starts with 6-9
It may be 10 digit or 11 digit length
If it is 11 digits it should starts with 0
91?0?[6-9][0-9]{9} //invalid
9107999999999 valid
07999999999 valid
7999999999 valid
(0|91)?[6-9][0-9]{9} //valid
12 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example:
import re
import time
for m in citr:
time.sleep(1)
print(m.group())
Example:
import re
import time
for m in citr:
time.sleep(1)
print(m.group())
13 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example:
import re
import time
if match!=None:
print("Target start with given pattern ")
else:
print("Sorry target not start with given pattern")
Example :2
import re
import time
if match!=None:
print("Target ends with given pattern ")
else:
print("Sorry target not ends with given pattern")
14 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Example:
import re
import time
if match!=None:
print("Target ends with given pattern ")
else:
print("Sorry target not ends with given pattern")
Example:
import re
import time
15 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
Validate mail-ID
1 |---------------2---------| |------3-----|
s hashikumarch.sssit @ gmail.com
tv9.gov.in
v6.edu.uk
eenadu.net
[a-zA-Z0-9][a-zA-Z0-9_.]+@[a-z0-9]+[[.][a-z]+]+
16 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
if match!=None:
print("Valid Mobile number")
else:
print("Sorry Invalid Mobile number")
import re
import time
Validating Mail-IDs
data=input("enter u r mail-id ")
match=re.fullmatch("[a-z0-9A-Z][a-zA-Z0-9._]+@[a-z0-9]+[[.][a-
z]+]+",data)
if match!=None:
print("Valid mail-id")
else:
print("Invalid Mail-id")
17 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
re.sub(pattern,replacement,target) str
It will return string after replacing the target will given
replacement data which are satisfied by the given pattern
import re
import time
m=re.sub
(r"\b\w{4}\b","***"," Hello my dear happy to see you")
print("type is : ",type(m))
print("Result is : ",m)
subn(pattern,replacement,target)-> tuple
It will return tuple object after replace the target with
specified pattern
t[0] will contain the result
t[1] will contain total no.of.replacements are done
import re
import time
18 | P a g e
SSST Computer Education
Besides R.S.Brothers Show Room
Kphb- Hyderabad - 9866144861
Python
split(pattern,target)-> list
Used to split the target based on the specified pattern
import re
import time
19 | P a g e