Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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.
0
NevSoFly
Asked:
NevSoFly
  • 3
  • 3
1 Solution
 
Terry WoodsIT GuruCommented:
The round brackets need escaping, otherwise they will be interpreted for their regular expression meaning (which is a group or lookahead, or lookbehind).

Also, given that with the third example you're trying to match 2 values within the string, it's important not to "consume" parts of the string for the first match such that the second match is missed. To achieve this, we need to use a lookahead.

This seems to work:
-?[0-9]+[°¹²³]+(?=[^(]*\))
0
 
NevSoFlyAuthor Commented:
I tested your suggestion on gskinner.com/regexr but it didn't return any matches.
0
 
Terry WoodsIT GuruCommented:
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.
screenshotAlso, in case it helps anyone else, the correct URL for the site's online tool is http://gskinner.com/RegExr/
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NevSoFlyAuthor Commented:
Thanks, deleting that extra space made all the difference in the world.
0
 
NevSoFlyAuthor Commented:
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.
0
 
Terry WoodsIT GuruCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now