I would like to split a line with multiple delimiters, but keep the delimiters in the resulting list. I think this is a useful thing to take the initial step in analyzing any formula, and I suspect there is a good Python solution.
Someone asked a similar question in Java here .
For example, a typical split is as follows:
>>> s='(twoplusthree)plusfour' >>> s.split(f, 'plus') ['(two', 'three)', 'four']
But I'm looking for a good way to add a plus back (or save it):
['(two', 'plus', 'three)', 'plus', 'four']
Ultimately, I would like to do this for each statement and parenthesis, so if there is a way to get
['(', 'two', 'plus', 'three', ')', 'plus', 'four']
all at one time, then everything is better.
source share