Python Regular Expression Search

0
243
Python regular expression search match

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

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

re.search()

First of all regular expression library should be imported:

import re

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

The re.search() takes the following arguments

re.search(patternstringflags=0)

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.

Input:

text = 'This text has an apple'
re.search('apple', text)

Output:

<_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?

Input:

print(re.search('APPLE',text))

Output:

 None

As you can see re.search 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.

Input:

print(re.search('APPLE',text,re.I))

Output:

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

re.match()

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

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

Input:

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

Output:

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

Input:

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

Output:

None

So there you have it use re.search() 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:

http://www.thedatalife.com/python-list-functions-methods-operations/

For official documentation on Python regular expression search go to:

https://docs.python.org/3.0/library/re.html

LEAVE A REPLY

Please enter your comment!
Please enter your name here