Posts

Showing posts from July, 2016

Regular Expressions with Python: Examples of common functions

Image
Regular Expressions are tools to filter and grab information from a heap of strings. Python has got a lot of toys to deal with it. The module "re". I am going to give you a quick ride through it. The ones that we use/gonna use the most.

So just immediately launch your favourite IDE and type "import re". Let's get started.

re.search:


for the object r where r = re.search(pattern, subject), r.start() - Gives the start point where the match was found. r.end() - Gives the end point where the match was found. r.group(0) - Returns the full matched string r.group(1) - Returns the matched string for the 1st group r.group(2) - Returns the matched string for the 2nd group
And so on....

Note : r.group() is same as r.group(0)

The number of groups available depends on the number of groups used in your pattern.

Example:


>>>regex=r"([a-zA-Z]+) (\d+)">>>re.search(regex,'July 4')<_sre.SRE_Matchobjectat0x0000000002E4D140>>>>r=re.s…