IKH

In Build Function

  • The module re contains several inbuilt functions to use regular expressions very easily in python applications.

compile()

  • This function compile a pattern into regex object.

Syntax

Python
import re
pattern = re.compile('ab')

finditer()

  • This function returns an iterator object which yields match object for every match.

Syntax

Python
matcher = pattern.finditer("abaababa")
  • On match object we can call the following methods:
    • start() ⇒ Returns start index of the match.
    • end() ⇒ Returns end+1 index of the match.
    • group() ⇒ Returns the matched string.

Example

Python
import re
count=0
pattern=re.compile("ab")
matcher=pattern.finditer("abaababa")
for match in matcher:
  count+=1
  print(match.start(),"...",match.end(),"...",match.group())
print("The number of occurrences: ", count)

Output

PowerShell
0 ... 2 ... ab
3 ... 5 ... ab
5 ... 7 ... ab
The number of occurrences:  3
  • We can pass pattern directly as argument to finditer() function.

Example

Python
import re
count=0
matcher=re.finditer("ab","abaababa")
for match in matcher:
  count+=1
  print(match.start(),"...",match.end(),"...",match.group())
print("The number of occurrences: ",count)

Output

PowerShell
0 ... 2 ... ab
3 ... 5 ... ab
5 ... 7 ... ab
The number of occurrences:  3

match()

  • We can use match function to check the given pattern at beginning of target string. If the match is available then we will get match object, otherwise we will get None.

Example

Python
import re
s=input("Enter pattern to check: ")
m=re.match(s,"abcabdefg")
if m!= None:
  print("Match is available at the beginning of the String")
  print("Start Index:",m.start(), "and End Index:",m.end())
else:
  print("Match is not available at the beginning of the String")

Output

PowerShell
Enter pattern to check: abc
Match is available at the beginning of the String
Start Index: 0 and End Index: 3
PowerShell
Enter pattern to check: bde
Match is not available at the beginning of the String

fullmatch()

  • We can use fullmatch() function to match a pattern to all of target string i.e. complete string should be matched according to given pattern. If complete string matched then this function returns match object otherwise it returns None.

Example

Python
import re
s=input("Enter pattern to check: ")
m=re.fullmatch(s,"ababab")
if m!= None:
  print("Full String Matched")
else:
  print("Full String not Matched")

Output

PowerShell
Enter pattern to check: ab
Full String not Matched
PowerShell
Enter pattern to check: ababab
Full String Matched

search()

  • We can use search() function to search the given pattern in the target string. If the match is available then it returns the match object which represents first occurrence of the match. If the match is not available then it returns None.

Example

Python
import re
s=input("Enter pattern to check: ")
m=re.search(s,"abaaaba")
if m!= None:
  print("Match is available")
  print("First Occurrence of match with start index:",m.start(),"and end index:",m.end())
else:
  print("Match is not available")

Output

PowerShell
Enter pattern to check: aaa
Match is available
First Occurrence of match with start index: 2 and end index: 5
PowerShell
Enter pattern to check: bbb
Match is not available

findall()

  • This function returns a list object which contains all occurrences.

Example

Python
import re
l=re.findall("[0-9]","a7b9c5kz")
print(l)

Output

PowerShell
['7', '9', '5']

sub()

  • sub means substitution or replacement.
  • In the target string every matched pattern will be replaced with provided replacement.

Example

Python
import re
s=re.sub("[a-z]","#","a7b9c5k8z")
print(s)

Output

PowerShell
#7#9#5#8#

subn()

  • It is exactly same as sub() except it can also returns the number of replacements.
  • This function returns a tuple where first element is result string and second element is number of replacements.

Example

Python
import re
t=re.subn("[a-z]","#","a7b9c5k8z")
print(t)
print("The Result String:",t[0])
print("The number of replacements:",t[1])

Output

PowerShell
('#7#9#5#8#', 5)
The Result String: #7#9#5#8#
The number of replacements: 5

split()

  • If we want to split the given target string according to a particular pattern then we should go for split() function.
  • This function returns list of all tokens.

Example

Python
import re
l=re.split(",","sunny,bunny,chinny,vinny,pinny")
print(l)
for t in l:
  print(t)

Output

PowerShell
['sunny', 'bunny', 'chinny', 'vinny', 'pinny']
sunny
bunny
chinny
vinny
pinny

Example

Python
import re
l=re.split("\.","www.narayantech.com")
for t in l:
  print(t)

Output

PowerShell
www
narayantech
com

^ symbol

  • We can use ^ symbol to check whether the given target string starts with our provided pattern or not.

Syntax

Python
res=re.search("^Learn",s)
  • if the target string starts with Learn then it will return match object, otherwise returns None.

Example

Python
import re
s="Learning Python is Very Easy"
res=re.search("^Learn",s)
if res != None:
  print("Target String starts with Learn")
else:
  print("Target String Not starts with Learn")

Output

PowerShell
Target String starts with Learn

$ symbol

  • We can use $ symbol to check whether the given target string ends with our provided pattern or not.

Syntax

Python
res=re.search("Easy$",s)
  • If the target string ends with Easy then it will return match object, otherwise returns None.

Example

Python
import re
s="Learning Python is Very Easy"
res=re.search("Easy$",s)
if res != None:
  print("Target String ends with Easy")
else:
  print("Target String Not ends with Easy")

Output

PowerShell
Target String ends with Easy

Note

  • If we want to ignore case then we have to pass 3rd argument re.IGNORECASE for search() function.

Example

Python
import re
s="Learning Python is Very Easy"
res=re.search("easy$",s,re.IGNORECASE)
if res != None:
  print("Target String ends with Easy by ignoring case")
else:
  print("Target String Not ends with Easy by ignoring case")

Output

PowerShell
Target String ends with Easy by ignoring case

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error