Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird backslash substitution in Ruby

I don't understand this Ruby code:

>> puts '\\ <- single backslash' # \ <- single backslash  >> puts '\\ <- 2x a, because 2 backslashes get replaced'.sub(/\\/, 'aa') # aa <- 2x a, because two backslashes get replaced 

so far, all as expected. but if we search for 1 with /\\/, and replace with 2, encoded by '\\\\', why do we get this:

>> puts '\\ <- only 1 ... replace 1 with 2'.sub(/\\/, '\\\\') # \ <- only 1 backslash, even though we replace 1 with 2 

and then, when we encode 3 with '\\\\\\', we only get 2:

>> puts '\\ <- only 2 ... 1 with 3'.sub(/\\/, '\\\\\\') # \\ <- 2 backslashes, even though we replace 1 with 3 

anyone able to understand why a backslash gets swallowed in the replacement string? this happens on 1.8 and 1.9.

like image 353
Peter Avatar asked Oct 09 '09 06:10

Peter


People also ask

What is backslash in Ruby?

In the context of a String, the backslash character \ tells Ruby that the character immediately following it is special. The backslash is frequently referred to as the escape character because it... escapes (I guess?) a character from being interpreted as normal.

How do you escape a double backslash?

As a corollary of the previous, you need to escape the escape character to avoid escaping the subsequent character. For example, '\' says to escape the second quote mark, leaving you with an unterminated string. However, '\\' results in a literal slash within the string.

What is backslash R regex?

The reason that you need four backslashes to represent one literal backslash is that "\" is an escape character in both R strings and for the regex engine to which you're ultimately passing your patterns. If you were talking directly to the regex engine, you'd use "\\" to indicate a literal backslash.


2 Answers

Quick Answer

If you want to sidestep all this confusion, use the much less confusing block syntax. Here is an example that replaces each backslash with 2 backslashes:

"some\\path".gsub('\\') { '\\\\' } 

Gruesome Details

The problem is that when using sub (and gsub), without a block, ruby interprets special character sequences in the replacement parameter. Unfortunately, sub uses the backslash as the escape character for these:

\& (the entire regex) \+ (the last group) \` (pre-match string) \' (post-match string) \0 (same as \&) \1 (first captured group) \2 (second captured group) \\ (a backslash) 

Like any escaping, this creates an obvious problem. If you want include the literal value of one of the above sequences (e.g. \1) in the output string you have to escape it. So, to get Hello \1, you need the replacement string to be Hello \\1. And to represent this as a string literal in Ruby, you have to escape those backslashes again like this: "Hello \\\\1"

So, there are two different escaping passes. The first one takes the string literal and creates the internal string value. The second takes that internal string value and replaces the sequences above with the matching data.

If a backslash is not followed by a character that matches one of the above sequences, then the backslash (and character that follows) will pass through unaltered. This is also affects a backslash at the end of the string -- it will pass through unaltered. It's easiest to see this logic in the rubinius code; just look for the to_sub_replacement method in the String class.

Here are some examples of how String#sub is parsing the replacement string:

  • 1 backslash \ (which has a string literal of "\\")

    Passes through unaltered because the backslash is at the end of the string and has no characters after it.

    Result: \

  • 2 backslashes \\ (which have a string literal of "\\\\")

    The pair of backslashes match the escaped backslash sequence (see \\ above) and gets converted into a single backslash.

    Result: \

  • 3 backslashes \\\ (which have a string literal of "\\\\\\")

    The first two backslashes match the \\ sequence and get converted to a single backslash. Then the final backslash is at the end of the string so it passes through unaltered.

    Result: \\

  • 4 backslashes \\\\ (which have a string literal of "\\\\\\\\")

    Two pairs of backslashes each match the \\ sequence and get converted to a single backslash.

    Result: \\

  • 2 backslashes with character in the middle \a\ (which have a string literal of "\\a\\")

    The \a does not match any of the escape sequences so it is allowed to pass through unaltered. The trailing backslash is also allowed through.

    Result: \a\

    Note: The same result could be obtained from: \\a\\ (with the literal string: "\\\\a\\\\")

In hindsight, this could have been less confusing if String#sub had used a different escape character. Then there wouldn't be the need to double escape all the backslashes.

like image 183
two-bit-fool Avatar answered Oct 02 '22 07:10

two-bit-fool


This is an issue because backslash (\) serves as an escape character for Regexps and Strings. You could do use the special variable \& to reduce the number backslashes in the gsub replacement string.

foo.gsub(/\\/,'\&\&\&') #for some string foo replace each \ with \\\ 

EDIT: I should mention that the value of \& is from a Regexp match, in this case a single backslash.

Also, I thought that there was a special way to create a string that disabled the escape character, but apparently not. None of these will produce two slashes:

puts "\\" puts '\\' puts %q{\\} puts %Q{\\} puts """\\""" puts '''\\''' puts <<EOF \\ EOF   
like image 32
sanscore Avatar answered Oct 02 '22 07:10

sanscore