musca999 musca999 - 4 months ago 10
Python Question

How to split a math formula that is composed of variables that contain dashes

Trying to split this line:

formula='%abc-def%+%hij-klm%/%opq+rst%-%uvw-xyz%


The variable are contained with the "%" signs and must remain intact. I want to split on +-/* without splitting the variable due to the '-' in the name.

Is there an easy way without having to use a for loop to scan each character?

re.compile("[\+\/\-\*]").split(formula)
splits the variables (no good):
['%abc', 'def%', '%hij', 'klm%', '%opq', 'rst%', '%uvw', 'xyz%']


re.compile("%[\+\/\-\*]%").split(formula)
loses the % (no good):
['%abc-def', 'hij-klm', 'opq+rst', 'uvw-xyz%']


I'm looking for something that'll yield:
['%abc-def%', '%hij-klm%', '%opq+rst%', '%uvw-xyz%']


Thanks,
Dan

Answer

You can prevent re.compile("%[\+\/\-\*]%").split(formula) from dropping the % characters by using lookarounds:

re.compile(r"(?<=%)[+/*-](?=%)").split(formula)

Another solution would be to split on [+/*-] but only if followed by an even number of % characters:

re.split(r'[+*/-](?=(?:(?:[^%]*%){2})*$)', formula)