Replacing words from strings except 'and' / 'or' / 'and not'

Nico Grubert nicogrubert at arcor.de
Thu Nov 25 11:33:29 EST 2004


>import sets
>KEYWORDS = sets.Set(['and', 'or', 'not'])
>
>query = "test and testing and not perl or testit or example"
>
>def decorate(w):
>    if w in KEYWORDS:
>        return w
>    return "*%s*" % w
>
>query = " ".join([decorate(w.strip()) for w in query.split()])
>  
>
Hi Diez,

thank you very much.
That's even easier than Skip's suggestion.
Also, thanks to Thomas for the regex tip!

cheers,
Nico



More information about the Python-list mailing list