RegEx for working with equations

Two more patterns that will be going into the RegExRX samples. These will allow you to work with mathematical equations.

The first will extract nested parenthesis, assuming the parens are balanced.


This pattern will find matching parenthesis, assuming that the parenthesis are balanced. Each iteration will find the innermost parenthesis first, then work outward. The sample text below starts with a nested mathematical equation, then shows each iteration as you remove the found matching parens, indicated with “eq#”.

(((3+5)+3)/(6-10)) -> (3+5), (6-10)
((eq1+3)/eq2) -> (eq1+3)
(eq3/eq2) -> will return the whole string

The next pattern will let you tokenize the numbers and operators:


This pattern will tokenize a mathematical equation to return either a number or an operator. This is designed to work in a loop on a string that contains nothing but the equation (no white space or text) With each iteration of the loop, you can determine if it is a number or operator by checking to see if subexpression 1 has content. If so, it’s a number. If not, subexpression 2 will contain an operator.

That’s fantastic. I’m really getting into these regular expressions. I’m working on a regular expressions tester, but I’m a little stumped how to write a regular expression to find regular expression patterns.

For example: “([±/*]\d+)|(\(.\))”

This simple expression finds two matches. How can I make an expression to identify that that expression finds two matches?

My apologies if this question warrants a new thread. I wasn’t sure.

You might want to save yourself some time and take a look at my app, RegExRX. :slight_smile:

Besides that, yes, a new thread was probably warranted. Please start one, and give me a better description as to what you’re trying to accomplish. I’ll do my best to help, but I’m unclear from your post.