Table of Contents
How to perform pattern matching in Python
Python provides a module referred as re for performing pattern matching using regular expression operations. It provides many different functions that allows you to check if a particular string matches a given regular expression. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, We cannot take combination of Unicode strings and 8-bit strings. Similarly, while doing substitution, the replacement string must be of the same type as both the pattern and the search string. The python's raw string notation is used for regular expression patterns.
There are five different functions provided in re module to perform pattern matching. They are as listed below.
- Using
re.search()
Function - Using
re.match()
Function - Using
re.fullmatch()
Function - Using
re.findall()
Function - Using
re.finditer()
Function
Method-1: Using re.search() Function
The search()
function of re module scans through the string looking for first location where the regular expression pattern matches the object. It will return the match object if the pattern is found. However, it will return None if the pattern is not found in the text.
The syntax of search()
method is as shown below. Here, pattern represents the pattern to search for in a string. The parameter flags is an optional which is used as modifiers to specify whether to ignore case or perform ASCII matching and many more.
re.search(pattern, text,flags=0)
Example 1 In this example, we will take list of patterns to be searched in the string to perform pattern matching.
# Importing re module
import re
# Initializing text and pattern
pattern=["Life","journey"]
s1="Life is a Journey not a destination"
# Iterating over the text to search for pattern
for i in pattern:
print("Searching ",i)
if(re.search(i,s1)):
print("Found ",i, "in the string - ", s1)
else:
print(i," not found in the string - ", s1)
Output
Searching Life
Found Life in the string - Life is a Journey not a destination
Searching journey
journey not found in the string - Life is a Journey not a destination
Method-2: Using re.match() Function
The match()
function of re module scans for the pattern only at the beginning of the string. This makes it different from the search()
function. It will return the match object, if pattern is found. However, it will return None , if the pattern is not found in the text.
The syntax of match()
method is as shown below. Here, pattern represents the pattern to search for in a string. The parameter flags is an optional which is used as modifiers to specify whether to ignore case or perform ASCII matching and many more.
re.match(pattern, text,flags=0)
Example 1 In this example, we will take list of patterns to be searched in the string to perform pattern matching.
# Importing re module
import re
# Initializing text and pattern
pattern=["Life","Journey"]
s1="Life is a Journey not a destination"
# Iterating over the text to search for pattern
for i in pattern:
print("Searching ",i)
if(re.match(i,s1)):
print("Match found at the beginning ---",i, "in found in the string - ", s1)
else:
print("Match not found at the beginning ---",i," not found in the string - ", s1)
Output
Searching Life
Match found at the beginning --- Life in the string - Life is a Journey not a destination
Searching Journey
None
Match not found at the beginning --- Journey not found in the string - Life is a Journey not a destination
Method-3: Using re.fullmatch() Function
From Python version 3.4 or higher the fullmatch()
function of re module scans for the pattern from a whole string. It will return the match object, if the whole string matches the pattern. However, it will return None
, if the pattern is not found in the string.
The syntax of fullmatch()
method is as shown below. Here, pattern represents the pattern to search for in a string. The parameter flags is an optional which is used as modifiers to specify whether to ignore case or perform ASCII matching and many more.
re.fullmatch(pattern, text,flags=0)
Example 1 In this example, we will take list of patterns to be searched in the string to perform pattern matching.
# Importing re module
import re
# Initializing text and pattern
pattern=["Life","Journey"]
s1="Life is a Journey not a destination"
s2="Life"
# Iterating over the text to search for pattern
for i in pattern:
print("Searching in s1 ",i)
if(re.fullmatch(i,s1)):
print("Match found ",i, "in found in the string - ", s1)
else:
print("Match not found ",i," not found in the string - ", s1)
# Iterating over the s2 to search for pattern
for i in pattern:
print("Searching in s2 ",i)
if(re.fullmatch(i,s2)):
print("**Match found** ")
else:
print("Match not found ")
Output
Searching in s1 Life
Match not found Life in the string - Life is a Journey not a destination
Searching Journey
None
Match not found Journey not found in the string - Life is a Journey not a destination
Searching in s2 Life
**Match found**
Searching in s2 Journey
Match not found
Method-4: Using re.findall() Function
The findall()
function of re module is used to search for all occurrences of a given pattern with in the text. This makes it different from search()
as search only finds the first occurrence of pattern. It will return the value of matched object, if the given pattern matches the text. However, it will return None , if the pattern is not found in the text.
The syntax of findall()
method is as shown below. Here, pattern represents the pattern to search for in a string. The parameter flags is an optional which is used as modifiers to specify whether to ignore case or perform ASCII matching and many more.
re.findall(pattern, text,flags=0)
Example 1 In this example, we will take list of patterns to be searched in the string to perform pattern matching.
# Importing re module
import re
# Initializing text and pattern
pattern=["Life","Journey"]
s1="Life is a Journey not a destination. Life is a journey with a problems to solve."
# Iterating over the text to search for pattern
for i in pattern:
print("Searching in s1 ",i)
print(re.findall(i,s1))
Output
Searching in s1 Life
['Life', 'Life']
Searching in s1 Journey
['Journey']
Method-5: Using re.finditer() Function
The finditer()
function of re module is used to search for all occurrences of a given pattern with in the text. It returns an iterator containing the match objects. This makes it different from findall()
function that returns the list of objects. It will return the matched object, if the given pattern matches the text. However, it will return None , if the pattern is not found in the text.
The syntax of finditer()
method is as shown below. Here, pattern represents the pattern to search for in a string. The parameter flags is an optional which is used as modifiers to specify whether to ignore case or perform ASCII matching and many more.
re.finditer(pattern, text,flags=0)
Example 1 In this example, we will take list of patterns to be searched in the string to perform pattern matching.
# Importing re module
import re
# Initializing text and pattern
pattern=["Life","Journey"]
s1="Life is a Journey not a destination. Life is a journey with a problems to solve."
# Iterating over the text to search for pattern
for i in pattern:
print("Searching in s1 ",i)
print(re.finditer(i,s1))
Output
Searching in s1 Life
<callable_iterator object at 0x7fc3edea6370>
Searching in s1 Journey
<callable_iterator object at 0x7fc3edef4ca0>
Summary
The knowledge of pattern matching with different available functions is important if you are working on some basic functionalities of a system in real time applications. You will frequently need to provide search functionality in web pages or standalone applications. The knowledge of pattern matching helps you implement that with very less efforts.In this tutorial, we covered the five different ways to perform pattern matching with an example. All in all, this tutorial, covers everything that you need to know in order to perform pattern matching in Python.
References