I have this regex: [A-Z][a-z]?d*|((?:[^()]*(?:(.*))?[^()]*)+)d+
This regex returns all simple and nested chemical formulas, for example: O2, SO4, Al2(SO4)3
I want to get these chemical formulas and use them to write a program to balance chemical equations.
Now I want to improve this regex so that it can separate elements and quantities and put them inside a list or tuple, for example: O2 -> (‘O’, 2)
SO4 -> ((‘S’, 1), (‘O’, 4))
Al2(SO4)3 -> (((‘Al’, 2), (((‘S’, 1), (‘O’, 4)),3)), 1)
This helps me to multiply the second value which is the same quantity by the first values and simplify the equation so that I can solve it. But I don’t know how to do it with regex if possible.
If it isn’t possible, please tell me a solution for it using python or tell me if you have a better way to simplify these equations or solve them.
I want to avoid using the library as much as possible.