MrJones
MrJones

Reputation: 3

Regex to parse equation

I'm trying to parse an equation such as

5x>=7-5y+4z

into a list of tuples with python:

[('', '5', 'x', '>='), ('', '7', '', ''), ('-', '5', 'y', ''), ('+', '4', 'z', '')]

I've managed to write a pattern (pattern = "[+-]?\d*[a-z]?[><=]*") to break the equation into groups, but I have no idea how to make it return tuples. Any help appreciated...

Upvotes: 0

Views: 298

Answers (1)

gmds
gmds

Reputation: 19885

I think you want this:

import re

pattern = re.compile(r'([+-]?)([0-9]+)([a-z]?)([><]?=?)')
re.findall(pattern, '5x>=7-5y+4z')

>>> [('', '5', 'x', '>='), ('', '7', '', ''), ('-', '5', 'y', ''), ('+', '4', 'z', '')]

Each instance of the regex passed to re.findall is put into a tuple, which is then further split into strings corresponding to each of the groups in the regex.

I took some liberties with the interpretation of the actual regex, since I'm not sure what the expected output for other cases would be (for example, would there be a 0x term?)

Upvotes: 2

Related Questions