我有以下字符串:
(some text) or ((other text) and (some more text)) and (still more text)
我想要一个python正则表达式,将它拆分为
['(some text)', '((other text) and (some more text))', '(still more text)']
我试过了,但没用:
haystack = "(some text) or ((other text) and (some more text)) and (still more text)"
re.split('(or|and)(?![^(]*.\))', haystack) # no worky
感谢您的帮助。你知道吗
我会用
re.findall
而不是re.split
。请注意,这只适用于深度为2的括号。你知道吗此解决方案适用于任意嵌套的括号,正则表达式不能(
s
是原始字符串):输出:
对于OP的真实测试用例:
输出:
你也可以检查一下
输出:
编辑
相关问题 更多 >
编程相关推荐