Spliting a string on non alpha characters

Mark Peters mpeters42 at gmail.com
Sat Sep 23 11:02:10 EDT 2006


> I'm relatively new to python but I already noticed that many lines of
> python code can be simplified to a oneliner by some clever coder. As
> the topics says, I'm trying to split lines like this :
>
> 'foo bar- blah/hm.lala' -> [foo, bar, blah, hm, lala]
>
> 'foo////bbbar.. xyz' -> [foo, bbbar, xyz]
>
> obviously a for loop catching just chars could do the trick, but I'm
> looking for a more elegant way. Anyone can help?

A simple regular expression would work:
>>> import re
>>> s = 'foo bar- blah/hm.lala'
>>> re.findall(r"\w+",s)
['foo', 'bar', 'blah', 'hm', 'lala']




More information about the Python-list mailing list