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

2009-03-27 Thread Tim Chase
Paul McGuire wrote: On Mar 27, 5:19 am, Tim Chase wrote: >>> import re >>> s = """a=1,b="0234,)#($)@", k="7" """ >>> rx = re.compile(r'[ ]*(\w+)=([^",]+|"[^"]*")[ ]*(?:,|$)') >>> rx.findall(s) [('a', '1'), ('b', '"0234,)#($)@"'), ('k', '"7"')] >>> rx.findall('a=1, *DODGY*SYNTAX* b=2') [(

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

2009-03-27 Thread Paul McGuire
On Mar 27, 5:19 am, Tim Chase wrote: > >>  >>> import re > >>  >>> s = """a=1,b="0234,)#($)@", k="7" """ > >>  >>> rx = re.compile(r'[ ]*(\w+)=([^",]+|"[^"]*")[ ]*(?:,|$)') > >>  >>> rx.findall(s) > >>  [('a', '1'), ('b', '"0234,)#($)@"'), ('k', '"7"')] > >>  >>> rx.findall('a=1, *DODGY*SYNTAX* b=

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

2009-03-27 Thread John Machin
On Mar 27, 9:19 pm, Tim Chase wrote: > >>  >>> import re > >>  >>> s = """a=1,b="0234,)#($)@", k="7" """ > >>  >>> rx = re.compile(r'[ ]*(\w+)=([^",]+|"[^"]*")[ ]*(?:,|$)') > >>  >>> rx.findall(s) > >>  [('a', '1'), ('b', '"0234,)#($)@"'), ('k', '"7"')] > >>  >>> rx.findall('a=1, *DODGY*SYNTAX* b=

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

2009-03-27 Thread Tim Chase
>>> import re >>> s = """a=1,b="0234,)#($)@", k="7" """ >>> rx = re.compile(r'[ ]*(\w+)=([^",]+|"[^"]*")[ ]*(?:,|$)') >>> rx.findall(s) [('a', '1'), ('b', '"0234,)#($)@"'), ('k', '"7"')] >>> rx.findall('a=1, *DODGY*SYNTAX* b=2') [('a', '1'), ('b', '2')] >>> I'm going to save this one and

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

2009-03-26 Thread R. David Murray
Paul McGuire wrote: > On Mar 26, 2:51 pm, "R. David Murray" wrote: > > OK, I've got a little problem that I'd like to ask the assembled minds > > for help with.  I can write code to parse this, but I'm thinking it may > > be possible to do it with regexes.  My regex foo isn't that good, so if > >

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

2009-03-26 Thread R. David Murray
John Machin wrote: > On Mar 27, 6:51 am, "R. David Murray" wrote: > > OK, I've got a little problem that I'd like to ask the assembled minds > > for help with.  I can write code to parse this, but I'm thinking it may > > be possible to do it with regexes.  My regex foo isn't that good, so if > >

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

2009-03-26 Thread John Machin
On Mar 27, 8:43 am, Terry Reedy wrote: > R. David Murray wrote: > > OK, I've got a little problem that I'd like to ask the assembled minds > > for help with.  I can write code to parse this, but I'm thinking it may > > be possible to do it with regexes.  My regex foo isn't that good, so if > > any

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

2009-03-26 Thread Terry Reedy
R. David Murray wrote: OK, I've got a little problem that I'd like to ask the assembled minds for help with. I can write code to parse this, but I'm thinking it may be possible to do it with regexes. My regex foo isn't that good, so if anyone is willing to help (or offer an alternate parsing su

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

2009-03-26 Thread Tim Chase
R. David Murray wrote: Tim Chase 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

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

2009-03-26 Thread Paul McGuire
On Mar 26, 2:51 pm, "R. David Murray" wrote: > OK, I've got a little problem that I'd like to ask the assembled minds > for help with.  I can write code to parse this, but I'm thinking it may > be possible to do it with regexes.  My regex foo isn't that good, so if > anyone is willing to help (or

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

2009-03-26 Thread Grant Edwards
On 2009-03-26, R. David Murray wrote: > Tim Chase wrote: >>r = re.compile(r""" >> (\w+) >> \s*=\s*( >> "(?:[^"]*)" >> | >> [^,]+ >> ) >> """, re.VERBOSE) >>results = [ >> (m.group(1), m.group(2).strip('"')) >> for m in r.finditer(s) >> ] >

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

2009-03-26 Thread R. David Murray
Tim Chase wrote: >r = re.compile(r""" > (\w+) > \s*=\s*( > "(?:[^"]*)" > | > [^,]+ > ) > """, re.VERBOSE) >results = [ > (m.group(1), m.group(2).strip('"')) > for m in r.finditer(s) > ] > > Things like internal quoting ('b="123\"456", c="1

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

2009-03-26 Thread John Machin
On Mar 27, 6:51 am, "R. David Murray" wrote: > OK, I've got a little problem that I'd like to ask the assembled minds > for help with.  I can write code to parse this, but I'm thinking it may > be possible to do it with regexes.  My regex foo isn't that good, so if > anyone is willing to help (or

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

2009-03-26 Thread Tim Chase
The challenge is to turn a string like this: a=1,b="0234,)#($)@", k="7" into this: [("a", "1"), ("b", "0234,)#($)#"), ("k", "7")] A couple solutions "work" for various pathological cases of input data: import re s = 'a=1,b="0234,)#($)@", k="7"' r = re.compile(r""" (?P\w+)