I am trying to write a regex pattern that removes all round brackets except the outer pair i.e (x)
-> (x)
, (x, (x,x,x))
-> (x,x,x,x)
, ((x), (x,x))
-> (x,x,x)
etc.
I know that I should use re.substitute(pattern, '', string)
yet I don't know how to specify that all round brackets except the external brackets. Could you guys please help?:3
CodePudding user response:
Just remove all the parens, and add the outer pair back:
>>> mystr = '(x,(x,x,x))'
>>> '(' mystr.replace('(', '').replace(')', '') ')'
'(x,x,x,x)'