python中的递归正则表达式?(Recursive regexp in python?)

编程入门 行业动态 更新时间:2024-10-28 08:24:53
python中的递归正则表达式?(Recursive regexp in python?)

我有字符串:

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)

I need to delete text within opened and coresponding closed bracket. So in example I need to delete

(e(fg)h)

In result I want to have

acd del ij)

I try to use next code:

re.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')

But python say:

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)'

Thanks Jerry and devnull! regex module for python instead of default re module solved my issue

import regex >>> regex.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)') 'acd del ij)'

更多推荐

本文发布于:2023-08-04 11:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   正则表达式   python   Recursive   regexp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!