musca999 musca999 - 1 year ago 43
Python Question

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

Trying to split this line:


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?

splits the variables (no good):
['%abc', 'def%', '%hij', 'klm%', '%opq', 'rst%', '%uvw', 'xyz%']

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%']


Answer Source

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


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

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