Python Regular Expression Search

Python regular expression search match

This post will discuss the Python regular expression search ( and match (re.match()) methods.

Regular expressions is a really broad subject and therefore will be broken down to multiple posts.

First of all regular expression library should be imported:

import re

The scans a string and returns a match object instance. If there is no match a None object is returned.

The takes the following arguments

pattern specifies the specific expression you want to search for.

string is the object which the scan will be conducted

flags are additional specification sfor the scan. The default is no flag.

Let’s do an example.


text = 'This text has an apple''apple', text)


<_sre.SRE_Match object; span=(17, 22), match='apple'>

Since there is an equivalent pattern in the string a match object was returned.

How about if we change the capitalization of the pattern. Will it still return a match object?





As you can see is case sensitive. This is where you use the flag argument for specifying additional specifications. Let’s add re.I as an argument which specifies that the scan is not case-sensitive.




<_sre.SRE_Match object; span=(17, 22), match='apple'>

You can see that a match object has been returned due to the flag we specified.

Various methods can be applied to the match object. It is more than a Boolean object (True or False ).

The match object contains information about the match including the original input string, the used regular expression and the match location.

Example of match object method is start() and end() which returns the location in the example above the output are 17 and 22 respectively.


The method re.match() only scans at the beginning of the string. Unlike the method scan across the whole string.

Let’s do an example of re.match() which also takes in the same arguments as


text2= 'Orange is first word'
re.match('orange', text2, re.I)


<_sre.SRE_Match object; span=(0, 6), match='Orange'>

As expected a match object has been produced as the Orange word appears in the fist section of the string.

Let’s see if the match works if the pattern doesn’t appear in the beginning.


text3= 'The last word is banana'
print(re.match('banana', text3))



So there you have it use if you are scanning across the whole string and re.match() if you want to scan only at the beginning of the string.

For more information on Python list and strings visit:

For official documentation on Python regular expression search go to:


Please enter your comment!
Please enter your name here