From: Eric Armstrong Date: 2006-07-29T07:49:49+09:00 Subject: Re: Finding "\" in a string Paul Battley wrote: > On 28/07/06, Eric Armstrong wrote: >> So it would appear that something strange is happening >> in the evaluation of the gsub replacement string. > > That's right. The replacement string understands back-references of > the form \1, \2 etc, so there's another level of escaping needed. > > "\\" <- one backslash > "\\\\" <- two backslashes > "\\\\\\\\" <- four backslashes, or two backslashes in replacement string > 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. Now then, we need two backslashes, so four of them /should/ have worked inside single quotes. (And if if takes 4 to make 1, why wasn't I effectively removing the existing quotes when I only supplied two.) I've seen a similar situation in Java, where it took 2 slashes to get one past the language parser, and it also took two to get one past the regular expression parser, so in that case it took four slashes to get one into the regular expression. That's the most complex case I've seen before this, but note that it was the _regular expression_ that needed four to get one. It wasn't the replacement string. The replacement string shouldn't be going through the reg exp parser, so it shouldn't require that many. It seems that it does, though. Huh?