Spliting a string on non alpha characters

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Sep 23 18:10:41 EDT 2006


stdazi:

The RE-based solutions look good. Here is a pair of alternative
solutions:

s1 = 'foo bar- blah/hm.lala'
r1 = ['foo', 'bar', 'blah', 'hm', 'lala']

s2 = 'foo////bbbar.. xyz'
r2 = ['foo', 'bbbar', 'xyz']

table = "".join((c if c.isalpha() else " " for c) in map(chr,
range(256)))
#table = "".join((" "+c)[c.isalpha()] for c in map(chr, range(256))) #
Py2.4
print s1.translate(table).split()
print s2.translate(table).split()

Or:

from itertools import groupby
print ["".join(gr) for he,gr in groupby(s1, str.isalpha) if he]
print ["".join(gr) for he,gr in groupby(s2, str.isalpha) if he]

Bye,
bearophile




More information about the Python-list mailing list