difficult regular expression

sismex01 at hebmex.com sismex01 at hebmex.com
Wed Oct 30 09:50:52 EST 2002


> From: Mike Rovner [mailto:mike at bindkey.com]
> 
> > I need to create a regular expression that will grab "the 
> > things that Cats eat".
> 
> import re
> pattern = re.compile( r'(\w+) eat (.*), and other foods\.' )
> source = " ".join((
> 'Here is a list of foods and consumers: Dogs eat <chicken>, <rice>,',
> '<steak>, and other foods. Cats eat <mice>, <rats>, 
> <rabbits>, <marmots>,',
> 'and other foods. Chickens eat <grain>, <corn>, <wheat>, and other',
> 'foods.  Wow, that's a lot of eating!'))
> found = pattern.findall( source )
> food={}
> for consumer, foodlist in found:
>   food[consumer]=[s[2:-1] for s in foodlist.split(',')]
> print food
> 
> >>> {'Cats': ['ice', 'rats', 'rabbits', 'marmots'], 
> 'Chickens': ['rain',
> 'corn', 'wheat'], 'Dogs': ['hicken', 'rice', 'steak']}
> 
> Mike
> 
> 

BWAAAHAHAHAHA!!!

Thanks a bunch man, I really needed a laugh today.

hah! Cats eat ice, chickens eat rain, and dogs "hicken". :-D

-gus




More information about the Python-list mailing list