Remove parentheses around integers in a string

I want to replace (number) with number following expression:

 4 + (3) - (7) 

It should be:

 4 + 3 - 7 

If the expression:

 2+(2)-(5-2/5) 

It should look like this:

 2+2-(5-2/5) 

I tried

 a = a.replace(r'\(\d\+)', '') 

where a is a string, but it does not work. Thanks!

+5
source share
1 answer

Python has a powerful re regular expression module that contains a lookup method:

 >>> import re >>> a = '2+(2)-(5-2/5)' >>> re.sub('\((\d+)\)', r'\1', a) '2+2-(5-2/5)' 
+7
source

Source: https://habr.com/ru/post/1237997/


All Articles