[Tutor] re.compile concatenating two searh words

Srinivas Iyyer srini_iyyer_bio at yahoo.com
Tue Sep 30 00:19:02 CEST 2008


Hi Tutors, 
I have a list with elements as strings. I want to search if any of these element strings has two words of my interest. How can I ask re.compile to look for both words. 

my words are 'good' and 'bad'.

import re

pat = re.compile('good'+'bad') 

a = ['Rama is a good boy','Raghu is a good boy','Sita is a good girl','Ravana is a bad boy','Duryodhan is a bad guy','good is an acceptable nature while bad is unwanted nature in a person']

pat.findall(a)

I want to search instances of both 'bad' and 'good' in a string. 

from the list 'a' above, I am expecting the result:

'good is an acceptable nature while bad is unwanted nature in a person'

 how can I define my pattern using re.compile. I appreciate your help. 

thank you. 

srini


      


More information about the Tutor mailing list