R. David Murray wrote:
Tim Chase <python.l...@tim.thechases.com> wrote:
   r = re.compile(r"""
     (\w+)
     \s*=\s*(
     "(?:[^"]*)"
     |
     [^,]+
     )
     """, re.VERBOSE)
   results = [
     (m.group(1), m.group(2).strip('"'))
     for m in r.finditer(s)
     ]

Thank you thank you.  I owe you a dinner if we are ever in the
same town (are you at Pycon?).

Attended PyCon '07 here in Dallas (my back yard), but didn't make '08 or '09


Grant Edwards wrote:
We'll wait until you need to modify that and then ask if you're
still grateful.  ;)

"There once was a programmer who had a problem..."

Indeed...I should have commented it a little :)

  r = re.compile(r"""
    (\w+)       # the variable name
    \s*=\s*     # the equals with optional ws around it
    (           # grab a group of either
    "(?:[^"]*)" # double-quotes around non-quoted stuff
    |           # or
    [^,]+       # stuff that's not a comma
    )           # end of the value-grab
    """, re.VERBOSE)

One of the benefits of re.VERBOSE allows making them a little less opaque. Unfortunately this version (the non-named-tagged version) includes the surrounding quotes in the second capture-group, so the list-comprehension has to strip off the surrounding quotes.

-tkc




--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to