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

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. You can find next tutorial link in the comment box.

Happy learning !!!

--

--

--

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What Sets cPanel Hosting Different From The Rest?

What Are Terraform Modules and How Do They Work?

A tale of retries using RabbitMQ

Rambling: Valve Index first impressions

Creating a Streamlit Calculator App: A Step-By-Step Python Tutorial

All we need is babababanana !

Classical Waterfall Model

“An Unprecedented Time For Gaming On The Mac” Says Apple VP Craig Federighi

A Gamecube

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gaurav Patil

Gaurav Patil

I am a keen learner and diligent teacher with special interest in mathematics and machine learning.

More from Medium

Tutorial 5 : OOPs in Python — Encapsulation

Variables in Python

DICTIONARY IN PYTHON

Python — How To Read One File Line by Line