tokenizing a string using more than 1 separator

David Goodger dgoodger at bigfoot.com
Sun May 21 10:54:11 EDT 2000


on 2000-05-21 09:59, Fernando (spamers at must.die) wrote:
> How can I tokenize a string using more than 1 separator? O:-)
> Apparentely, split can only use on separator...

use re.split:

>>> import re
>>> s = "one,two;three:four,five;six:seven"
>>> re.split("[,;:]", s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']
>>> sep = re.compile("[,;:]")
>>> sep.split(s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']

I *love* Python. While other languages do what you say, Python does what you
mean. I've never had so much code run perfectly, first time. :>

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come! honest! just as soon as my kids give me some free time!)




More information about the Python-list mailing list