Recently I had to update a lot of user access rules in RSA IGL again.
If I have to do such batch jobs, I usually create an XML file and import it through the provided user interface.
This time I had to adjust the rule design, which basically meant parsing SQL where statements and changing the way they work technically, without changing the result of the where clause.
One important part of that was to keep of course the logical order, but my colleagues had made excessive use of unnecessary parenthesis during the rule implementation.
So here is a small method, which will replace unmatched parenthesis in a provided text for you.
I can not recommend to try and do this with regular expressions. You will most likely fail and even if you succeed it bears a certain risk opposed to using a simple Stack.
However, be careful when using this. I had to make sure not to change the meaning of the rules, which I accomplished using a mix of regular expressions and the below method.
I replaced with regex and then used this to clean up afterwards. Luckily I could follow this approach.