How Do I Split On The Space-separated Math Operators Here?
I have a user calculator that accepts both numbers and user-defined variables. My plan is to split the statement on a space-delimited operator, e.g. ' + ': import re query = 'house
Solution 1:
Using re.split
with capturing parentheses around the split pattern seems to work fairly effectively. As long as you have the capturing parens in the pattern the patterns you are splitting on are kept in the resulting list:
re.split(' ([+-/*^]|//) ', query)
Out[6]: ['house-width', '+', '3', '-', 'y', '^', '(5', '*', 'house length)']
(PS: I'm assuming from your original regex you want to catch //
integer division operators, you can't do that with a single character class, so I've moved that outside the character class as a special case.)
Post a Comment for "How Do I Split On The Space-separated Math Operators Here?"