json - Python regex issues - Attempting to parse string -
i want take string this:
enabled='false' script='var name=\'bob\'\\n ' index='0' value='' and convert json type format:
{'enabled': 'false', 'script': 'var name=\'bob\'\\n ', 'index': '0', 'value': ''} but cannot life of me figure out regex or combination of splitting string produce result.
the values can have specials characters in them , escape single quotes , backslashes.
is there way regex in python stop after finding first match?
for example, this:
import re re.findall('[a-za-z0-9]+=\'.*\'', line) will match entire string instead , won't stop @
['stripprefix=\'false\'', ....] like to.
first of all, assume have mistake in input string: quote before "bob" should escaped.
if assumption correct use regex code this:
>>> line = r"""enabled='false' script='var name=\'bob\'\\n ' index='0' value=''""" >>> re.findall(r"([a-za-z]*)='((?:[^'\\]|\\.)*)'\s*", line) [('enabled', 'false'), ('script', "var name=\\'bob\\'\\\\n "), ('index', '0'), ('value', '')] [^'\\]match symbol except quote , backslash\\.match backslash and 1 more symbol([^'\\]|\\.)matches either of previous cases(?:[^\\]|\\.)same doesn't capture match result (check https://docs.python.org/2.7/library/re.html)(?:[^'\\]|\\.)*repeat times
Comments
Post a Comment