From: Brian Candler Date: 2010-01-13T22:11:25+09:00 Subject: Re: Ruby Regex Sriram Varahan wrote: > Hello, > > I have a string as a = "&0&1" > > I need to pass this value with the ampersand escaped to another command > in my program. > > So I tried something like this: > > irb(main):038:0> a.gsub(/&/,"\\&") > => "&0&1" That's because \& has a special meaning in a replacement string ("the matched string"). Either use a block to provide the replacement value (which doesn't do the backslash replacement), or put two backslashes in the replacement string. irb(main):002:0> a.gsub(/&/) { "\\&" } => "\\&0\\&1" irb(main):003:0> a.gsub(/&/, "\\\\&") => "\\&0\\&1" -- Posted via http://www.ruby-forum.com/.