I'm not sure what you're really looking for, but
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=\"[email protected]\",mailer=\"mta\",client_name=\"example.org,[194.177.17.24]\",resolved=OK,to=\"[email protected]\",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))
gives you
[('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', '"[email protected]"'), ('mailer', '"mta"'),
('client_name', '"example.org,[194.177.17.24]"'), ('resolved', 'OK'),
('to', '"[email protected]"'), ('direction', '"in"'),
('message_length', '6832079'), ('virus', '""'), ('disposition', '"Accept"'),
('classifier', '"Not,Spam"'),
('subject', '"=?windows-1255?B?Rlc6IEZ3OiDg5fDp5fog+fno5fog7Pf46eHp7S3u4+Tp7SE=?="')
]
You might want to clean up the quoted strings afterwards (using mystring.strip("'\"")
).
EDIT: This regex now also correctly handles escaped quotes inside quoted strings (a="She said \"Hi!\""
).
Explanation of the regex:
(\w+)=((?:"(?:\\.|[^\\"])*"|'(?:\\.|[^\\'])*'|[^\\,"'])+)
(\w+)
: Match the identifier and capture it into backreference no. 1
=
: Match a =
(
: Capture the following into backreference no. 2:
(?:
: One of the following:
"(?:\\.|[^\\"])*"
: A double quote, followed by either zero or more of the following: an escaped character or a non-quote/non-backslash character, followed by another double quote
|
: or
'(?:\\.|[^\\'])*'
: See above, just for single quotes.
|
: or
[^\\,"']
: one character that is neither a backslash, a comma, nor a quote.
)+
: repeat at least once, as many times as possible.
)
: end of capturing group no. 2.