Tokenize a string or split on steroids

Bob Follek b.follek at verizon.net
Sat Mar 9 06:30:40 EST 2002


"Fernando Rodríguez" wrote:
> 
> I need to tokenize a string using several separator characters, not just one
> as split().
> 
> For example, I want a function that returns ['one', 'two'] when given the
> string '{one}{two}' .
> 
> How can I do this? O:-)

Take a look at the re library module. For example:

>>> import re
>>> x = re.compile('\w+')
>>> x.findall('{one}{two}')
['one', 'two']

If you're unfamiliar with regular expressions, here's a good starting
point: http://py-howto.sourceforge.net/regex/regex.html



More information about the Python-list mailing list