Brian Candler wrote:

> That is, in a replacement string, if you backslash-escape a backslash 
> you get a single backslash. That allows you to have literally \1 if 
> that's what you need.
> 
> So a literal backslash is \\, and the first capture is \1
> 
> So what you want is \\\1, to get a backslash followed by the first 
> capture. However, that is represented in a string literal as '\\\\\\1' 
> (which generates a 4 character string) because a string literal also has 
> backslash escaping.
> 
>>> '\\\\\\1'.size
> => 4
>>> puts "\\:{}=#~".gsub(/([\\\:\~\=\#\{\}])/, '\\\\\\1')
> \\\:\{\}\=\#\~
> => nil
> 
> Take a suggestion from me: save your sanity and use the block form 
> instead :-)
> 
>>> puts "\\:{}=#~".gsub(/([\\\:\~\=\#\{\}])/) { "\\#{$1}" }
> \\\:\{\}\=\#\~
> => nil

ThanksBrian!
I know the block form.
So the problem is the backslash escape in string:
  '\\\1' == '\\\\1'  => true

-- 
Posted via http://www.ruby-forum.com/.