Gsub backslashes (escaping and feedback)

Consider the following snippet:

puts 'hello'.gsub(/.+/, '\0 \\0 \\\0 \\\\0') 

Prints ( as seen on ideone.com ):

 hello hello \0 \0 

This was very surprising because I expected to see something like this:

 hello \0 \hello \\0 

My argument is that \ is an escape character, so you write \\ to get a literal backslash, so \\0 is a literal backslash \ followed by 0 , etc. Obviously, this is not how gsub interprets this, so can someone explain what is happening?

And what do I need to do to get the replacement I want above?

+4
ruby regex replace escaping backreference
Jun 12 '10 at 11:48
source share
1 answer

Escaping is limited when using single quotes rather than double quotes:

 puts 'sinlge\nquote' puts "double\nquote" 

"\0" is the null character (used in C to determine the end of the line), where '\0' is "\\0" , so both are 'hello'.gsub(/.+/, '\0') and 'hello'.gsub(/.+/, "\\0") returns "hello" , but 'hello'.gsub(/.+/, "\0") returns "\000" . Now 'hello'.gsub(/.+/, '\\0') returning 'hello' is a ruby โ€‹โ€‹trying to cope with programmers without preserving the difference between single and double quotes. In fact, this has nothing to do with gsub : '\0' == "\\0" and '\\0' == "\\0" . Following this logic, no matter how you think about it, this is how Ruby sees other lines: both '\\\0' and '\\\\0' are equal to "\\\\0" , which (when printed) give you \\0 . Since gsub uses \x to insert the number of matches x, you need the escape \x method, which is \\x , or in its lowercase representation: "\\\\x" .

Therefore line

 puts 'hello'.gsub(/.+/, "\\0 \\\\0 \\\\\\0 \\\\\\\\0") 

really leads to

 hello \0 \hello \\0 
+4
Jun 12 '10 at 13:21
source share



All Articles