From: Brian Candler Date: 2010-01-14T03:25:18+09:00 Subject: Re: Ruby Regex Marnen Laibow-Koser wrote: > The problem is that Ruby "helpfully" does another level of > escaping It's necessary because it lets you use sequences like \n in double-quoted strings, and \" if you want a double-quote, and \#{expr} if you want literally # { expr } rather than interpolation. > so that "\\&" is equivaIent to "\&", whereas it should simply > take the escape at face value and consider it equivalent to the two > characters \ and &. Which is what happens in single-quoted strings. But you can't put any control character sequences like \n in those. > For real fun, try concatenating two strings, the first of which ends in > a backslash. It's insane. a = "abc\\" # that's a string ending with one backslash b = "def" c = a + b Looks OK to me. > But Ruby has its own special brand of idiocy here. Even Perl and PHP > get this right. Perl is *exactly* the same. #!/usr/bin/perl print "abc\\\n"; This prints abc\ - same as Ruby would. It was probably unfortunate that gsub uses sequences like \1 and the like in the substitution side though. But that's what perl does: #!/usr/bin/perl $_ = "ab&de\n"; s/&/\&/; print; That prints ab&de, which is the problem the OP was grappling with. -- Posted via http://www.ruby-forum.com/.