[Tutor] re.compile concatenating two searh words

Jerry Hill malaclypse2 at gmail.com
Tue Sep 30 01:18:01 CEST 2008


On Mon, Sep 29, 2008 at 6:19 PM, Srinivas Iyyer
<srini_iyyer_bio at yahoo.com> wrote:
> 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'.

If this really reflects your requirements, regular expressions are not
a good choice in the first place.  Instead, I would use some of
python's built in string functionality, like this:

>>> 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']

>>> for item in a:
	if 'good' in item and 'bad' in item:
		print item

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



-- 
Jerry


More information about the Tutor mailing list