Regular Expression in Python: Greedy match

When you use re.findall(“(.*):” to get the strings ending with full-colon(:), you may not always get the expected result if your string has multiple full-colons. Lets run the command before we talk.

Concentrate on the commands put inside single colon. (You can neglect the rest as it is part of my effort to avoid creating a python file for running this code)

Screen Shot 2014-10-15 at 6.12.15 PMOutput:Screen Shot 2014-10-15 at 6.12.21 PM

What happened here is python did a greedy search and found maximum string ending with a full-colon. If that is what you desired, stop reading further.. 🙂

If you expected a list of all the strings ending with full-colon, change your command to re.findall(“(.*?):”

As you can see, I added a question mark which will force python to avoid greedy approach.

Before we part, lets make sure python is ok with this change..


Screen Shot 2014-10-15 at 6.11.40 PM


Screen Shot 2014-10-15 at 6.12.00 PMLOVE YOU PYTHON…

Published by


A strong believer of: 1. Knowledge is power 2. Progress comes from proper application of knowledge 3. Reverent attains wisdom 4. For one's own salvation, and for the welfare of the world

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s