Ticker

6/recent/ticker-posts

Python: Important functions of re module

  1. match()
  2. fullmatch()
  3. search()
  4. findall()
  5. finditer()
  6.  sub()
  7. subn()
  8. split()
  9. compile()

1. 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.

Eg:

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

Output:

D:\python_classes>py test.py
Enter pattern to check: abc
Match is available at the beginning of the String
Start Index: 0 and End Index: 3
D:\python_classes>py test.py
Enter pattern to check: bde
Match is not available at the beginning of the String

2. 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.

Eg:

1) import re 
2) s=input("Enter pattern to check: ") 
3) m=re.fullmatch(s,"ababab") 
4) if m!= None: 
5) print("Full String Matched") 
6) else: 
7) print("Full String not Matched") 

Output:

D:\python_classes>py test.py
Enter pattern to check: ab
Full String not Matched
D:\python_classes>py test.py
Enter pattern to check: ababab
Full String Matched

3. 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

Eg:

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

Output:

D:\python_classes>py test.py
Enter pattern to check: aaa
Match is available
First Occurrence of match with start index: 2 and end index: 5
D:\python_classes>py test.py
Enter pattern to check: bbb
Match is not available

4. findall():

To find all occurrences of the match.

This function returns a list object which contains all occurrences.

Eg:

1) import re 
2) l=re.findall("[0-9]","a7b9c5kz") 
3) print(l) 
Output: ['7', '9', '5']

5. finditer():

Returns the iterator yielding a match object for each match.

On each match object we can call start(), end() and group() functions.

Eg:
1) import re 
2) itr=re.finditer("[a-z]","a7b9c5k8z") 
3) for m in itr: 
4) print(m.start(),"...",m.end(),"...",m.group()) 
Output:
D:\python_classes>py test.py
0 ... 1 ... a
2 ... 3 ... b
4 ... 5 ... c
6 ... 7 ... k
8 ... 9 ... z

6. sub():

sub means substitution or replacement

re.sub(regex,replacement,targetstring)

In the target string every matched pattern will be replaced with provided replacement.

Eg:

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

Output: #7#9#5#8#

Every alphabet symbol is replaced with # symbol

7. 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.

    (resultstring, number of replacements)

Eg:

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

Output:

D:\python_classes>py test.py
('#7#9#5#8#', 5)
The Result String: #7#9#5#8#
The number of replacements: 5

8. 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.

Eg:

1) import re 
2) l=re.split(",","sunny,bunny,chinny,vinny,pinny") 
3) print(l) 
4) for t in l: 
5) print(t) 

Output:

D:\python_classes>py test.py
['sunny', 'bunny', 'chinny', 'vinny', 'pinny']
sunny
bunny
chinny
vinny
pinny

^ symbol:

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

Eg:

res=re.search("^Learn",s)

if the target string starts with Learn then it will return Match object,otherwise returns None.

test.py:

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

Output: Target String starts with Learn

$ symbol:

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

Eg: res=re.search("Easy$",s)

If the target string ends with Easy then it will return Match object,otherwise returns None.

test.py:

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

Output: Target String ends with Easy

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

Eg: res = re.search("easy$",s,re.IGNORECASE)

test.py:

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

Output: Target String ends with Easy by ignoring case

"Python Important Functions"

"Python Important Modules"

"Functions In Re Module Python"

"Re Module Functions In Python"

"Python Re Module Functions"