In Python, how can I split a string using multiple delimiters and know which delimiter was used to separate any two elements?
E.g. in the following example taken from this post:
>>> a='Beautiful, is; better*than\nugly'
>>> import re
>>> re.split('; |, |\*|\n',a)
['Beautiful', 'is', 'better', 'than', 'ugly']
how can I determine that the separator which separated 'is' and 'better' was '; '?
awk
has a useful way to accomplish this with patsplit(string, array [, fieldpat [, seps ] ])
, where seps
is an array that holds the separator that separated two elements. In this case, seps[1]
would be ', ', seps[2]
would be '; ', seps[3]
would be '*', and seps[4]
would be '\n'. I didn't see a similar feature in re.split
.