I am working on an Advanced Search feature where the expression I need to evaluate will look something like this with the parenthesis in place:
((Loan number
If I understand you correctly, you already got the expression evaluator. What you need is to split the evaluations according to the parenthesis. I'd use a loop, in which I'd find inner parenthesis groups, using this regex:
\(([^()]*)\)
Then, if found, replace them with the result of your evaluation routine, and repeat until a final string remains, without parenthesis.
Pseudo code:
Find a string enclosed by (), not containing any ()
If found
Replace it with the evaluated value of the string (including parenthesis)
Go again
Return result
About the unused parenthesis, let them be treated the same. They'll end up in your evaluation routine as a single value.
Check this fiddle. Instead of evaluating it returns a random number, 0
or 1
, but it demonstrates the logic.
Hope this helps.
Regards.