該解決方案適用於任意嵌套的括號,其中一個正則表達式不能(s
是原始字符串):
from pyparsing import nestedExpr
def lst_to_parens(elt):
if isinstance(elt,list):
return '(' + ' '.join(lst_to_parens(e) for e in elt) + ')'
else:
return elt
split = nestedExpr('(',')').parseString('(' + s + ')').asList()
split_lists = [elt for elt in split[0] if isinstance(elt,list)]
print ([lst_to_parens(elt) for elt in split_lists])
輸出:
['(some text)', '((other text) and (some more text))', '(still more text)']
對於OP真實的測試案例:
s = "(substringof('needle',name)) or ((role eq 'needle') and (substringof('needle',email))) or (job eq 'needle') or (office eq 'needle')"
輸出:
["(substringof ('needle' ,name))", "((role eq 'needle') and (substringof ('needle' ,email)))", "(job eq 'needle')", "(office eq 'needle')"]
正則表達式不能很好地處理任意嵌套的內容。除了您向我們展示的示例之外,可能會有更多層嵌套括號。對於這種情況,使用解析器可能會比正則表達式更進一步。 –
這可能有所幫助:https://stackoverflow.com/questions/26633452/how-to-split-by-commas-that-are-not-within-parentheses –
這可能也是有用的:https://stackoverflow.com/questions/4284991/parsing-nested-parentheses-in-python-grab-content-by-level – perigon