I'm looking to remove mathematically unneeded parentheses from a mathematical equation string. I need to do this either, and preferably, in PostgreSQL 6 or VBA.
For example, I have the following string value in a PostgreSQL database:
PercentileRank((([bp47244]+([bp47229][ttm]))/(AvgAeTe([bp48918]))))
And I need it to look like this (edited/corrected):
PercentileRank(([bp47244]+[bp47229][ttm])/AvgAeTe([bp48918]))
I'd prefer a function or query in PostgreSQL, but a VBA solution could work.
Note PercentileRank()
and AvgAeTe()
are functions. This [bp47244]
and [bp47229][ttm]
each represent single numbers/variables, but they could be expressed in any way, like [abc123]
and [xyz321][ttm]
. I see a lot of examples out there, but I don't see one using PostgreSQL or VBA that works for me, so I thought it would be a good question.
Of course I am looking for a general solution that can be applied to any equation.
I'm working on this now, so if I find an answer before one is posted here, I'll share; however, I am not good at regex (not that the solution has to use regex).
Thanks!
UPDATE: I'm working off this logic:
Let L be operator immediately left of the left parenthesis, or nil
Let R be operator immediately right of the right parenthesis, or nil
If L is nil and R is nil:
Redundant
Else:
Scan the unparenthesized operators between the parentheses
Let X be the lowest priority operator
If X has lower priority than L or R:
Not redundant
Else:
Redundant
from this link: Remove redundant parentheses from an arithmetic expression
I'll code something up in VBA that follows this logic and post an answer.