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.