multi split function taking delimiter list
Sam Pointon
free.condiments at gmail.com
Tue Nov 14 18:41:12 EST 2006
On Nov 14, 7:56 pm, "martins... at gmail.com" <martins... at gmail.com>
wrote:
> Hi, I'm looking for something like:
>
> multi_split( 'a:=b+c' , [':=','+'] )
>
> returning:
> ['a', ':=', 'b', '+', 'c']
>
> whats the python way to achieve this, preferably without regexp?
pyparsing <http://pyparsing.wikispaces.com/> is quite a cool package
for doing this sort of thing. Using your example:
#untested
from pyparsing import *
splitat = Or(":=", "+")
lexeme = Word(alphas)
grammar = splitat | lexeme
grammar.parseString("a:=b+c")
#returns (the equivalent of) ['a', ':=', 'b', '+', 'c'].
--Sam
More information about the Python-list
mailing list