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

Building a User-friendly Search Experience with Algolia

Deploy a proxy server squid on RedHat-like systems — Ansible modules yum, template, service and…

SQL Performance Issues

10 Things I Wish I’d Known Before I Started Coding

How To Build Sustainable Serverless Applications!

Basic web app authentication with Firebase

Concrete Boolean and Conway’s Game of Life: A Tutorial

Swyft Update July, 2020

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 2 : OOPs in Python — more examples of objects and classes

Python Branching and Loops

Slice Operator: Python

Make To Do List Program Using Python