Python Tutorial

Thursday, August 11, 2011

python reqular expression re.findall()

re.findall() is very similar to re.search. Only difference is re.search() 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.cse@gmail.com test
test@yahoo.com test_tester@yahoo.com test-123@yahoo.com') print m



Output:
['rr', 'rr']
['rr', 'rr', 'RR']
['12', '89']
['test.cse@gmail.com', 'test@yahoo.com', 'test_tester@yahoo.com', '123@yahoo.com']

0 comments:

Post a Comment