From: Paul Battley Date: 2006-07-29T08:10:09+09:00 Subject: Re: Finding "\" in a string On 28/07/06, Eric Armstrong wrote: > Very helpful. At least now I have a good idea that it > can be made to work. Now let's see if I can dial in my > understanding with respect to replacement strings... > > The fact that \1, \2 are recognized /shouldn't/ matter, > because the tokenizer really ought to ignore any \x > sequence where the x isn't a known special character. > But it's rapidly becoming apparent that the tokenizer > takes any \x and translates it to /something/, so we > need \\ to get one \ in the string. In fact, the tokenizer *does* ignore non-special sequences: "\\foo".gsub("\\", "\\x") # => "\\xfoo" And if there's nothing else left in the string, that works, too: "\\foo".gsub("\\", "\\") # => "\\foo" However, in order to be able to produce a literal \1, it also needs to understand \\. Therefore, this gives the same effect: "\\foo".gsub("\\", "\\\\") # => "\\foo" But I missed a trick earler. We only actually need six backslashes to emit two: the end of the string will terminate the sequence as in the second example: "\\foo".gsub("\\", "\\\\\\") # => "\\\\foo" Is it getting clearer? Paul.