This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org 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


2026-02-14 16:06