I have string:
acd (e(fg)h) ij)
我需要删除打开和对应关闭括号中的文本。例如,我需要删除
(e(fg)h)
我想要的结果是:
acd del ij)
我会尝试使用下面的代码:
我尝试使用以下代码:
re.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')
但Python说:
sre_constants.error: unexpected end of pattern
I have string:
acd (e(fg)h) ij)
我需要删除打开和对应关闭括号中的文本。例如,我需要删除
(e(fg)h)
acd del ij)
我尝试使用以下代码:
re.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')
但Python说:
sre_constants.error: unexpected end of pattern
感谢Jerry和devnull!使用Python中的regex模块而不是默认的re模块解决了我的问题。
import regex
>>> regex.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')
'acd del ij)'