1

Any ideas how to replace:

..((....))..

With:

..(...)..

Be aware, it is not a straight up replace of "((" with "(". The expression must determine that the child brace pair being removed is contained directly with the parent pair, with no other content.

Bonus points if anyone can figure out how to function recursively, e.g. "(((...)))" to "(...)"

Phil B
  • 387
  • 5
  • 15

1 Answers1

1

You can use this:

([(]*)(?:\([^)]*\))([)]*)

You just need to replace groups with empty string if even first group size is equal to second group or else use the minimum one.

Test:

(ABC)
((ABC))
(((ABC)))
((ABC)a)

Match Information:

Match 1
Full match  0-5 `(ABC)`
Group 1.    0-0 ``
Group 2.    5-5 ``
--> Hence, no update required
Match 2
Full match  6-13    `((ABC))`
Group 1.    6-7 `(`
Group 2.    12-13   `)`
--> As Group 1 and Group 2 size is same, replace those values with '' resulting to '(ABC)
Match 3
Full match  14-23   `(((ABC)))`
Group 1.    14-16   `((`
Group 2.    21-23   `))`
--> Same in this case as well
Match 4
Full match  24-30   `((ABC)`
Group 1.    24-25   `(`
Group 2.    30-30   ``
--> As group 1 and group 2 are not of same size, reduce to the min one which is group 2 (size 0) and hence no update required leaving it to '((ABC)A)'

Demo

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39