Python regular expression question

Thomas Guettler guettli at thomas-guettler.de
Mon Jun 9 13:47:53 CEST 2003


On Sun, Jun 08, 2003 at 07:34:27PM +1000, DJ wrote:
> Hi
> 
> I cant figure out a re that matches a string in a sentance.
> The string can be anywhere in the sentance
> 
> eg
> 
> "dog"
> 
> I walked my dog today

Maybe \w* is what you are looking for:

import re 
sentence="I walked my dog today" 
words=re.findall(r'\w*', sentence) 
print words

#skip empty strings:
for word in words:
    if not word:
        continue
    print word
 
-->
['I', '', 'walked', '', 'my', '', 'dog', '', 'today', '']
I
walked
my
dog
today

 thomas

-- 
Thomas Guettler <guettli at thomas-guettler.de>
http://www.thomas-guettler.de






More information about the Python-list mailing list