あなたが本当に何を求めているのかわかりませんが、
import re
data = "date=2010-05-09,time=16:41:27,device_id=FE-2KA3F09000049,log_id=0400147717,log_part=00,type=statistics,subtype=n/a,pri=information,session_id=o49CedRc021772,from=\"prvs=4745cd07e1=example@example.org\",mailer=\"mta\",client_name=\"example.org,[194.177.17.24]\",resolved=OK,to=\"example@example.org\",direction=\"in\",message_length=6832079,virus=\"\",disposition=\"Accept\",classifier=\"Not,Spam\",subject=\"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?=\""
pattern = r"""(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)"""
print(re.findall(pattern, data))
あなたにあげる
[('date', '2010-05-09'), ('time', '16:41:27'), ('device_id', 'FE-2KA3F09000049'),
('log_id', '0400147717'), ('log_part', '00'), ('type', 'statistics'),
('subtype', 'n/a'), ('pri', 'information'), ('session_id', 'o49CedRc021772'),
('from', '"prvs=4745cd07e1=example@example.org"'), ('mailer', '"mta"'),
('client_name', '"example.org,[194.177.17.24]"'), ('resolved', 'OK'),
('to', '"example@example.org"'), ('direction', '"in"'),
('message_length', '6832079'), ('virus', '""'), ('disposition', '"Accept"'),
('classifier', '"Not,Spam"'),
('subject', '"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?="')
]
後で引用符で囲まれた文字列をクリーンアップすることをお勧めします ( を使用mystring.strip("'\"")
)。
編集: この正規表現は、引用符で囲まれた文字列 ( ) 内のエスケープされた引用符も正しく処理するようになりましたa="She said \"Hi!\""
。
正規表現の説明:
(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)
(\w+)
: 識別子を一致させ、後方参照番号にキャプチャします。1
=
: 抹茶=
(
: 以下を後方参照番号にキャプチャします。2:
(?:
: 次のいずれか:
"(?:\\.|[^\\"])*"
: 二重引用符の後に次の 0 個以上が続きます: エスケープ文字または非引用符/非バックスラッシュ文字、その後に別の二重引用符
|
: また
'(?:\\.|[^\\'])*'
: 単一引用符については、上記を参照してください。
|
: また
[^\\,"']
: バックスラッシュ、カンマ、引用符のいずれでもない 1 文字。
)+
: 少なくとも 1 回、可能な限り何度でも繰り返します。
)
:撮影グループ番号終了 2.