Python Tutorial

Thursday, August 11, 2011

python reqular expression re.findall()

re.findall() is very similar to Only difference is looking for first instance but re.findall() for all instance match with this expression.
Lets see some example:
import re 

#find all one or more r
m=re.findall(r'r+','tomorrow rrow RRO')
print m

#find all one or more r ignorecase
m=re.findall(r'r+','tomorrow rrow RRO',re.IGNORECASE)
print m

#find all two consecutive digits
m=re.findall(r'\d\d','tomorrow123  foo89')
print m

#find all email from string
m=re.findall(r'[\w.-_]+@[\w.]+','test 123 . @ test') print m

['rr', 'rr']
['rr', 'rr', 'RR']
['12', '89']
['', '', '', '']


Post a Comment