Similar to this question, I am trying to match a string with *two optional values, both of with are surrounded by parentheses and are of unknown length. Examples of the string in question are:
4
4(2(3))
4(2(3)(1))
4(2(3)(1))(6(5))
1(2(3(4(5(6(7(8)))))))
-4(2(3)(1))(6(5))
There is always a digit that may be negative, and at most 2 items in parenthesis. I would like to capture them in groups, so for the last item, the groups would be:
group 1: -4
group 2: 2(3)(1)
group 3: 6(5)
Adding another optional group to the regex seems to break down:
(-?\d+)(?:\((.*)?\))?(?:\((.*)?\))?
^ ^ ^
group 1 group 2 group 3