Please note: This wiki is currently running in test mode after an attack on January 5 2013. All passwords were reset, so you will have to use the password recovery function to get a new password. To edit wiki pages, please log in first. See the wiki attack description page for more details. If you find problems, please report them to the pydotorg-www mailing list.

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)