Home > database >  Remove brackets if the content inside is a number
Remove brackets if the content inside is a number

Time:09-27

Is there any way to remove the brackets () if the content inside .isnumeric()

I do know a little bit of RegEx but I'm unable to find a way to do it using RegEx.

Example:

input = '((1) (1)) 2 (1 2) ((2))'
output = somefunction(input)

Here the output should look like

(1 1) 2 (1 2) 2

CodePudding user response:

import re

x = '((1) (1)) 2 (1 2) ((2))'
re.sub(r'(\()([\d*\.] )(\))', r"\2", x)

But this will give you (1 1) 2 (1 2) (2)

Can maybe use re.subn to do this until number of replacements are 0

  • Related