python - python中的递归正则表达式?

标签 python regex recursion

我有字符串:

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/

相关文章:

python - Django - 模型字段的动态唯一测试

Python:帮助改进递归函数

Javascript:文本字符串到字符数组

Ruby 语句 - 不区分大小写?

javascript - 如何向我的斐波那契数列添加停止指令?

获得最少项数总和的算法

python - 使用 Python 在 ElasticSearch 中添加 @timestamp 字段

python - 一个非常简单的 python subprocess.Popen 任务带来无尽的问题

python - 使用 zipfile 模块在 python 中创建带有压缩的 zip 文件

regex - 更改日期格式的 PHP 正则表达式