I want to parse equations and get a list of tuples. For example, when I enter
2x = 4+3y,
I want to get
[('', '2', 'x', '='), ('','4','',''), ('+','3','y','')]
This is my regex so far:
([+-]*)([0-9]+)([a-z]*)([<=>]*)
It works fine for the above query but it does not capture equations like
2 = x +3y
, (where x does not have any coefficient)
How do I capture that?
If you change the quantifier on the coefficient from
+
(one or more) to*
(zero or more) then you should get the result you are after. You will also get an empty string match due to all the quantifiers now being*
but you can filter out that match.Note: whilst this solves your direct question this is not a good approach to parsing infix equations.
How about this regex?
It separates all operands and operators. (and inside operands it also splits number and variable).
For testing the regex I normally use https://regex101.com/ so you can build regex with live changes there.