删除字符串中整数的括号

时间:2022-10-28 15:05:53

I want to replace (number) with just number in an expression like this:

我想用这样的表达式中的number替换(number):

4 + (3) - (7)

It should be:

它应该是:

4 + 3 - 7

If the expression is:

如果表达式是:

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

it should be like this:

它应该是这样的:

2+2-(5-2/5)

I tried

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

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

其中a是一个字符串,但它不起作用。谢谢!

1 个解决方案

#1


7  

Python has a powerful module for regular expressions, re, featuring a substitution method:

Python有一个强大的正则表达式模块,re,具有替换方法:

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

#1


7  

Python has a powerful module for regular expressions, re, featuring a substitution method:

Python有一个强大的正则表达式模块,re,具有替换方法:

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