Functions in ‘re’ module(Part I)- Match vs Search

Functions in ‘re’ module(Part I)- Match vs Search

re module

To use patterns in python we need to use re module in our code.
Syntax

import re

This will enable us to define different patterns and to call different methods of re module.

Methods in Regular Expressions

match()

The match() method only find matches if they occur at the start of the string being searched.
The re.match() returns a match object on success, None on failure.

Methods of match object

MethodsUse/Description
group()If it is called without argument then it will return the substring, which had been matched by the complete regular expression.
start()It returns the string index where the regular expression started matching in the string.
end()It returns the string index where the regular expression ended matching in the string.
span()It returns a tuple with the string index where the regular expression started matching in the string and ended matching.
groups()It returns all matching subgroups in a tuple

Syntax

 re.match(pattern,string,flags)

Where,
pattern – It is the regular expression to be matched
String – It is the data string , which is checked to match the pattern anywhere in the string.
Flags – This parameter is optional.It is used to determine or control behavior of patterns.At a time we can specify multiple flags by using bitwise OR (|).

Examp
le :

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date :30th January 2019

Description : Program to show use of match()
"""

import re
res= re.match(r'i','i am itvoyagers page')
res2=re.match(r'itvoyagers','i am itvoyagers page')
if res:
    print("match found",res.group())
else:
    print("match not found")
if res2:
    print("match found",res2.group())
else:
    print("match not found")

OUTPUT

match found i
match not found

Note: res gives output because match functions matches the beginning of string whereas res2 gives match not found because ‘itvoyagers’ is not present at the start of string.

search()

The search() method scans entire string, to find the pattern.
The re.search() returns a match object on success, None on failure.

Syntax

 re.search(pattern,string,flags)

Where,
pattern – It is the regular expression to be matched
String – It is the data string , which is checked to search the pattern anywhere in the string.
Flags – This parameter is optional.It is used to determine or control behavior of patterns.At a time we can specify multiple flags by using bitwise OR (|).

Example :

"""
Author : ITVoyagers (https://itvoyagers.in/)

Date :30th January 2019

Description : Program to show use of search()
"""

import re
res= re.search(r'i','i am itvoyagers page')
res2=re.search(r'itvoyagers','i am itvoyagers page')
if res:
    print("match found",res.group())
else:
    print("match not found")
if res2:
    print("match found",res2.group())
else:
    print("match not found")

OUTPUT

match found i
match found itvoyagers

Note: res and res2 both gives output because search functions matches pattern by scanning entire string.

You can also check following other posts on regular expression using python here

Leave a Comment