This is because when the string is created \\& is converted into \&. In a substitution string \& refers to the part of the origional string which matches the regexp.
Why am I not able to escape the & in the string with an a \ ? If I do this:
This is because when the string is created \\& is converted into \&. In
a substitution string \& refers to the part of the origional string
which matches the regexp.
This is because when the string is created \\& is converted into \&. In a substitution string \& refers to the part of the origional string which matches the regexp.
Why am I not able to escape the & in the string with an a \ ? If I do this:
That's exactly the right conversion! Apparently you've been fooled by IRB which uses #inspect. Using puts to print the converted string sometimes helps avoid confusion:
str = "123&456"
=> "123&456"
str.gsub( /&/, '\\\\&' )
=> "123\\&456"
puts str.gsub( /&/, '\\\\&' )
123\&456
=> nil
Note also:
puts '\\\\&'
\\&
=> nil
You need four backslashes because you want two backslashes in the substitution string. The reason for that is that a backslash is a meta character in a substitution string (for example if you use '\\2' for the second group and '\\&' for the complete match) and thus you have to escape it in order to get it literally.
Another way that works is to use the block form
str.sub!( /\&/) {"\\&"}
I've noticed that people quite often recommend to use the block form of sub/gsub to cope with escaping. Please don't do that: the block form is less performant than the direct form. It's applicable where the substitution string (or rather pattern) has to change for each substitution. All other cases should use the argument form.