Checking if string starts with list element

David Goodger dgoodger at bigfoot.com
Tue Aug 15 22:06:29 EDT 2000


One point to be aware of: if you're only looking at first words, many of the
examples given (splitting on whitespace & punctuation), work fine. But if
your search strings are not simple words, as your original posting
suggested,

    romans = ['aqueduct', 'sanitation', 'roads', 'irrigation', 'medicine',
              'education', 'wine', 'public baths', 'order']

Then "first words" method won't hack it with 'public baths'. (Nor if you're
searching within the string 'wines of the world'.) I'd go with Rob Hooft's
suggestion for the general case, especially if speed is not crucial. (Not
that Rob's suggestion is slow, just slower. But if speed were paramount,
you'd be writing it in C anyhow! ;-)

For an even more general solution, to Rob's line,

    R=re.compile('('+string.join(romans,'|')+')')

I would add re.IGNORECASE:

    R=re.compile('('+string.join(romans,'|')+')', re.IGNORECASE)

This will match capitalized words as well.

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list