我有字符串:
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)'
关于python - python中的递归正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23183777/