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

3
  • possible duplicate of Python Regex to find a string in double quotes within a string Commented Sep 21, 2014 at 2:09
  • 2
    This regex "([^"]*)" would do the job. Commented Sep 21, 2014 at 2:10
  • 1
    this seems like an XML node attributes - I'd parse it with XML parser instead of regex Commented Sep 21, 2014 at 2:12

2 Answers 2

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'}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.