0

I've a format like this:

att1="value 1" att2="value 2" att3="value 3" 

for example

level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET" 

Can I use regex to parse this? inside the values I won't have any embedded quotes but I'll have spaces

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
adglopez
  • 66
  • 1
  • 7
  • possible duplicate of [Python Regex to find a string in double quotes within a string](http://stackoverflow.com/questions/9519734/python-regex-to-find-a-string-in-double-quotes-within-a-string) – Avinash Raj Sep 21 '14 at 02:09
  • 2
    This regex `"([^"]*)"` would do the job. – Avinash Raj Sep 21 '14 at 02:10
  • 1
    this seems like an XML node attributes - I'd parse it with XML parser instead of regex – Alex P. Sep 21 '14 at 02:12

2 Answers2

1

Through findall function , you could get the values inside double quotes.

>>> import re
>>> m = 'level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET"'
>>> s = re.findall(r'"([^"]*)"', m)
>>> for i in s:
...     print i
... 
Information
127.0.0.1
GetByName
Action completed
/customers/foo
GET
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1
import xml.dom.minidom

def parsed_dict(attrs):
    return dict(xml.dom.minidom.parseString('<node {}/>'.format(attrs)).firstChild.attributes.items())

print parsed_dict('level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET"')

{u'clientAddr': u'127.0.0.1', u'level': u'Information', u'url': u'/customers/foo', u'action': u'GetByName', u'message': u'Action completed', u'method': u'GET'}
Alex P.
  • 30,437
  • 17
  • 118
  • 169