From: Robert Klemme Date: 2010-01-14T17:05:04+09:00 Subject: Re: Ruby Regex 2010/1/13 Marnen Laibow-Koser : > I'm glad to know that Ruby's backslash handling is not as weird as I'd > thought.  Thanks for the correction. :-) For me there is actually something weird about Ruby's escape handling - but it's something else: in some circumstances Ruby allows you to *omit* a backslash which is meant to be convenient (I believe) but which leads to a certain inconsistency: irb(main):014:0> '\1' # this might be seen as surprising => "\\1" irb(main):015:0> '\\1' => "\\1" We can get a single backslash by just using one, but if we need more backslashes we need to escape: irb(main):027:0> '\\1' => "\\1" irb(main):028:0> '\\\1' => "\\\\1" irb(main):029:0> '\\\\1' => "\\\\1" irb(main):030:0> '\\\\\1' => "\\\\\\1" irb(main):031:0> '\\\\\\1' => "\\\\\\1" For double quoted strings we always need to use two backslashes at least if followed by a digit: irb(main):016:0> "\1" => "\x01" irb(main):017:0> "\\1" => "\\1" and irb(main):018:0> '\n' # this might be seen as surprising => "\\n" irb(main):019:0> '\\n' => "\\n" but irb(main):020:0> "\n" # a single newline => "\n" irb(main):021:0> "\\n" # backslash and n => "\\n" Bottom line for me: I do not exploit the '\1' case and have made it a habit to use two backslashes whenever I need a literal backslash in a string. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/