split string at commas respecting quotes when string not in csv format

Tim Chase python.list at tim.thechases.com
Thu Mar 26 22:32:45 CET 2009


R. David Murray wrote:
> Tim Chase <python.list at 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







More information about the Python-list mailing list