NevSoFly
asked on
Need help matching and solving for numbers with exponents.
I am looking for a pattern to match numbers from an expression that have exponents and are enclosed in ():
(64²xy) should match 64²
(6xy/-2²³xy) should match -2²³
(64²xy/2²yx) should match 64² and 2²
(6xy/-2²³+ab) should match -2²³
6xy/-2²³xy should not match anything as the expression is not enclosed in ()
The closest I have come to solving this is "([\-]?[0-9]+[°¹²³]+)". This pattern matches all of the above example text but I only want it to match the first four examples.
Finally I would like to replace the matched text with a number that solves the exponent.
(64²xy) should match 64²
(6xy/-2²³xy) should match -2²³
(64²xy/2²yx) should match 64² and 2²
(6xy/-2²³+ab) should match -2²³
6xy/-2²³xy should not match anything as the expression is not enclosed in ()
The closest I have come to solving this is "([\-]?[0-9]+[°¹²³]+)". This pattern matches all of the above example text but I only want it to match the first four examples.
Finally I would like to replace the matched text with a number that solves the exponent.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I tried it myself; the copy and paste of the pattern added a space character to the end, which broke it. Once I removed the space, it worked.
Also, in case it helps anyone else, the correct URL for the site's online tool is http://gskinner.com/RegExr/
Also, in case it helps anyone else, the correct URL for the site's online tool is http://gskinner.com/RegExr/
ASKER
Thanks, deleting that extra space made all the difference in the world.
ASKER
Sorry, I forgot about the second part of my question. I would like to replace the matched text with a number that solves the exponent.
My specialty is regular expressions (I don't program in .NET) so you're probably best asking a separate question if you need help with that.
ASKER