From: Paul Lutus Date: 2006-09-16T08:20:56+09:00 Subject: Re: No regex backreference with four backslashes gabriel.birke@gmail.com wrote: > > Paul Lutus schrieb: > >> >> To find out how your strings are being parsed, print them out. / ... > I've done that already, the test was only to show the problem: I could > not escape chars in the numbers string with a backslash. > > Anyway, I found the solution, it's five backslashes instead of four. > That's a bit counter-intuitive, maybe someone can explain it. > Especially when these two are compared: > > numbers.gsub(/(2|4)/,'\\ \\1') > numbers.gsub(/(2|4)/,'\\\\\1') > > I expected that when I remove the space from the first expression, that > my characters would get quoted. instead, the four backslashes get > interpreted as two escaped backslashes and the 1 as a literal > character. Can somebdoy shed some light on the how and why of this > case? Sure. Parsing these strings is a trivial exercise. Each adjacent pair of backslashes collapses into one literal backslash, and any orphan backslashes are associated with the character to its immediate right. > Especially, why the solution with the five backslashes doesn't > yield double backlashes in the result string? To sort out how Ruby is parsing your strings, *print* *them* *out.* puts '\\\\1' \\1 # meaning: a backslash and an escaped '1' puts '\\ \\1' \ \1 # meaning a backslash, a space, and an escaped '1' puts '\\\\\1' \\\1 # meaning two backslashes and an escaped '1' Oh, by the way. You haven't said what you are trying to accomplish. -- Paul Lutus http://www.arachnoid.com