On Mon, Nov 22, 2010 at 10:38 AM, Robert Klemme
<shortcutter / googlemail.com> wrote:
> On Mon, Nov 22, 2010 at 12:27 AM, Ammar Ali <ammarabuali / gmail.com> wrote:
>> On Sun, Nov 21, 2010 at 11:02 PM, Brian Candler <b.candler / pobox.com> wrote:
>>> Ralph Shnelvar wrote in post #962847:
>>>>  "\\1\\2\\3".gsub(/\\/,"\\\\")
>>>
>>> Here you are replacing one backslash with one backslash.
>>>
>>> The trouble is, in the *replacement* string, '\1' has a special meaning
>>> (insert the value of the first capture). Because of this, a literal
>>> backslash is backslash-backslash.
>>
>> That's a keen observation, but the fact that they happen to be
>> back-references doesn't seem to play a part in this situation.
>>
>>>> "\\a\\b\\c".gsub(/\\/,"\\\\")
>> => "\\a\\b\\c"
>>>> "\\a\\b\\c".gsub(/\\/,"\\\\\\")
>> => "\\\\a\\\\b\\\\c"
>
> The key point to understand IMHO is that a backslash is special in
> replacement strings. So, whenever one wants to have a literal
> backslash in a replacement string one needs to escape it and hence
> have to backslashes. Coincidentally a backslash is also special in a
> string (even in a single quoted string). So you need two levels of
> escaping, makes 2 * 2 = 4 backslashes on the screen for one literal
> replacement backslash.

Actually, 3 backslashes will yield one backslash. The first two result
in one (escaped), and the third one, escaped by the previous escaped
backslash ends up being one. My second example showed this, using 6
backslashes instead of 8. Using 4 backslashes works because the second
pair yields and escaped backslash, but it is not necessary.

Regards,
Ammar