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 patternI 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)'更多推荐
发布评论