From: Brian Candler Date: 2010-01-08T21:55:32+09:00 Subject: Re: Stopping String Escaping. Phil Cooper-king wrote: > so after the sub section I loose two of the back slashes > [code] > text.sub!(/##code\((\S+)\)(.+?)##code/m, code) > [/code] Ah yes, backslashes have a special interpretation in the string-replacement part of a (g)sub too: \1 means the first capture, \2 means the second capture etc, so \\ means a single backslash. Note that the replacement string here is two backslashes: >> puts "abc".sub(/b/, "\\\\") a\c => nil The easy solution is to use the block form of sub instead. >> puts "abc".sub(/b/) { "\\\\" } a\\c => nil You could simplify your code if you rewrote to use the block form of gsub anyway. text.gsub!(/#>code\((\S+)\)(.+?)#>code/m) do |snip| ... make a string containing the marked-up code end -- Posted via http://www.ruby-forum.com/.