我需要使用正则表达式混合解析python中的用户输入并遍历正则表达式的结果方面的帮助。输入示例如下所示:
KeylessBuy=f and not (Feedback.color = green or comment.color=green)
and not "BIN State".color = white and comment="got it right"
分割结果应为:
KeylessBuy=f
Feedback.color = green
comment.color=green
"BIN State".color = white
comment="got it right"
因此,仅选择直接围绕“ =”-符号的那些部分。我尝试了其他方法:
r'(\w+\s{0,}(?<!=)={1,2}(?!=)\s{0,}\w+)'
r'|("(.*?)"\s{0,}(?<!=)={1,2}(?!=)\s{0,}\w+)'
r'|("(.*?)"\s{0,}(?<!=)={1,2}(?!=)\s{0,}"(.*?)")'
r'|(\w+\s{0,}(?<!=)={1,2}(?!=)\s{0,}"(.*?)")'
r'|(\w+\s{0,}\.\w+\s{0,}(?<!=)={1,2}(?!=)\s{0,}"(.*?)")',
这仅“几乎”给出了正确的答案。任何帮助深表感谢!的确感谢。标记
您可以使用以下内容:
>>> import re
>>> s = '''KeylessBuy=f and not (Feedback.color = green or comment.color=green)
and not "BIN State".color = white and comment="got it right"'''
>>> m = re.findall(r'(?:[\w.]+|"[^=]*)\s*=\s*(?:\w+|"[^"]*")', s)
>>> for x in m:
... print x
KeylessBuy=f
Feedback.color = green
comment.color=green
"BIN State".color = white
comment="got it right"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句