\(([^\\(\\)]+)\)我上面的正则表达式捕获了表单的每组括号之间的所有内容(Hello OR there) AND (big AND wide AND world)我懂了Hello OR therebig AND wide AND world但是当括号内的术语中有括号时它会下降(Hello OR there AND messing(it)up) AND (big AND wide AND world)退货itbig AND wide AND world我想Hello OR there AND messing(it)upbig AND wide AND world我不确定正则表达式是否可行,或者最好的方法是什么?
1 回答

呼啦一阵风
TA贡献1802条经验 获得超6个赞
您可以使用以下模式:
\(((?:[^()]+|(?R))*+)\)
如果可能,(?R)子表达式递归整个模式。
您可以在这里尝试。
输入:
(Hello OR there AND messing(it)up) AND (big AND wide AND world)
捕获的组是:
Group 1. 47-79 `Hello OR there AND messing(it)up`
Group 1. 86-108 `big AND wide AND world`
如果您使用的是Python,则可以使用以下regex模块:
import regex
mystring = '(Hello OR there AND messing(it)up) AND (big AND wide AND world)'
print(regex.findall('(?V1)\(((?:[^()]+|(?R))*+)\)',mystring))
印刷:
['Hello OR there AND messing(it)up', 'big AND wide AND world']
添加回答
举报
0/150
提交
取消