Tutorial 6 : Regular Expressions in Python- performing matches in regex

Gaurav Patil
3 min readOct 1, 2021

--

Photo by Ilya Pavlov on Unsplash

Missed previous tutorial ? — Click here

You can directly navigate to the topic of your interest by clicking on topic name given below :

1. Methods to get the matches

2. Methods to get information about the matching string

1. Methods to get the matches

a) match() — to find match at the beginning

b) search() — to find match at any location (only first match is returned)

c) findall() — to get all the matches as a list

d) finditer() — to get all the matches as an iterator

As we always do , let’s understand this using examples :

match() and search() return none if no match can be found. If they’re successful, a match object is returned, containing information about the match: where it starts and ends and the substring it matched.

2. Methods to get information about the matching string

a) group() — Returns the matching string

b) start() — Returns the starting index of the matching string

c) end() — Returns the ending index of the matching string

d) span() — Returns a tuple (start, end) index of the matching string

Let’s try this with examples:

Errors above simply suggest that findall() and finditer() methods cannot be used to extract information using group(), span() etc. So to get information of matching strings we should be using search() and match() methods.

Take a deep breathe and move to the next tutorial.

Happy learning !!!

--

--

Gaurav Patil
Gaurav Patil

Written by Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.

Responses (1)