带引号、等号和点的属性/值对正则表达式

4 投票
4 回答
675 浏览
提问于 2025-04-18 13:43

我需要帮助,想在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,}"(.*?)")',

这个方法“几乎”给出了正确的答案。任何帮助都非常感谢!真的谢谢你。马克

4 个回答

0

这个应该可以用。你可以从索引1获取匹配的组。

((\"[^=]*|[\w\.]+)\s*=\s*(\w+|\"[^"]*\"))

示例

示例代码:

import re
p = re.compile(ur'((\"[^=]*|[\w\.]+)\s*=\s*(\w+|\"[^"]*\"))')
test_str = u"KeylessBuy=f and not (Feedback.color = green or comment.color=green) \nand not \"BIN State\".color = white and comment=\"got it right\""

re.findall(p, test_str)
0

你可以试试下面这个正则表达式,

>>> str = '''
... 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 ]+\")?[\w.]+\s*=\s*(?:\w+)?(?:\"[\w ]+\")?', str)
>>> m
['KeylessBuy=f', 'Feedback.color = green', 'comment.color=green', '"BIN State".color = white', 'comment="got it right"']
>>> for item in m:
...     print item
... 
KeylessBuy=f
Feedback.color = green
comment.color=green
"BIN State".color = white
comment="got it right"

示例

1

我用下面的代码让它符合你的要求。

((?:"[^"]+")?[\w\.]+?) ?= ?((?:"[^"]+")|\w+)

你可以在这里查看正则表达式的演示。

3

你可以使用下面的内容:

>>> 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"

撰写回答