Asking for Help: How can I split a string, but without breaking the quotes

I'm trying to split a string, but without breaking the quotes

x = '"a,b", c, d, "e,f,h", "i,j,k"'
re.sub('"([^"]*)"', "\g<1>", x) 
-> 'a,b, c, d, e,f,h, i,j,k' # Ok for me

re.sub('"([^"]*)"', "|\g<1>|", x)
-> '|a,b|, c, d, |e,f,h|, |i,j,k|'   # To show the separation of the groups, also Ok

re.sub('"([^"]*)"', "\g<1>".replace(",", "|"), x)
-> 'a,b, c, d, e,f,h, i,j,k' # The same result!
-> 'a|b, c, d, e|f|h, i|j|k' # It doesn't suppose to replace all "," with "|" like here?

Answer: No, it is doing what you told it to do. To realize this you must know that arguments are evaluated before they are passed to the functions. But what is the value of this...?

"\g<1>".replace(",", "|")

It is "\g<1>", of course, as there are no commas to replace in "\g<1>". re.sub therefore receives '"([^"]*)"' and "\g<1>" as arguments, which is the exact equivalent of the first call.


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/How can I split a string, but without breaking the quotes? (last edited 2009-11-07 01:15:45 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.