0

I have a string as "Zinc Oxide (ZnO))" and I need a regex to balance the parenthesis and result should be "Zinc Oxide (ZnO)". I have not much worked on regex. Could anyone please help me with the regex?

String.replace would be solution but I need a regex to balance parenthesis for any string like in the example.

Any help on this would be appreciated.

Sasi
  • 39
  • 2
  • 1
    No actual regex will do that for you. Sorry. – dotvav Aug 12 '15 at 10:03
  • Thank you for sharing your thoughts. So I should be doing the validation manually by iterating over and remove the unnecessary round brackets. Am I correct? – Sasi Aug 12 '15 at 10:23
  • @Sasi See this post for a possible solution: http://stackoverflow.com/a/16874430/2516301 – vefthym Aug 12 '15 at 10:25

1 Answers1

0

I totally agree with comments below your question, this is not possible to create regex to balance parentheses in String BUT if your Strings to 'balance' are as simple as your example, you can test something like this:

"Zinc Oxide (ZnO))".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)","");

DEMO

There is a \w\w\w in demo, instead of \\w{1,100} like in example, because this syntax in lookbehind is allowed only (at least as I know) in Java. Lookbehind is zero-length points, so in most regex flavours lookbehind block ((?<=...),(?=...),etc.) cannot contain quantifiers (+*?) or intervals ({1,2}), but in Java it is allowed if interval has min and max length (so only in {n} or {min,max} form).

But as I said above, it will work only with most simple input!! No nested parentheses, etc. Example in Java:

public class Test {
    public static void main(String[] args) {
        System.out.println("Zinc Oxide (ZnO))".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)",""));
        System.out.println("Zinc Oxide ((ZnO))".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)",""));
        System.out.println("Zinc Oxide ((ZnO)".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)",""));
        System.out.println("Zinc Oxide (ZnO)))".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)",""));
        System.out.println("Zinc Oxide (((ZnO))".replaceAll("\\((?!\\w+\\))|(?<!\\(\\w{1,100})\\)",""));
    }
}

with output:

Zinc Oxide (ZnO)
Zinc Oxide (ZnO)
Zinc Oxide (ZnO)
Zinc Oxide (ZnO)
Zinc Oxide (ZnO)
m.cekiera
  • 5,365
  • 5
  • 21
  • 35