python按“and”和“or”拆分,但不在括号中

2024-06-17 10:00:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串:

(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

感谢您的帮助。你知道吗


Tags: orandno字符串textremoresome
3条回答

我会用re.findall而不是re.split。请注意,这只适用于深度为2的括号。你知道吗

>>> import re
>>> s = '(some text) or ((other text) and (some more text)) and (still more text)'
>>> re.findall(r'\((?:\((?:\([^()]*\)|[^()]*)*\)|[^()])*\)', s)
['(some text)', '((other text) and (some more text))', '(still more text)']
>>> 

此解决方案适用于任意嵌套的括号,正则表达式不能(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')"]

你也可以检查一下

import re
s = '(some text) or ((other text) and (some more text)) and (still more text)'
find_string = re.findall(r'[(]{2}[a-z\s()]*[)]{2}|[(][a-z\s]*[)]', s)
print(find_string)

输出:

['(some text)', '((other text) and (some more text))', '(still more text)']

编辑

find_string = re.findall(r'[(\s]{2}[a-z\s()]*[)\s]{2}|[(][a-z\s]*[)]', s)

相关问题 更多 >