From: mark sparshatt Date: 2005-04-19T02:16:37+09:00 Subject: Re: String.sub strangeness Belorion wrote: > Forgive me if my Monday fog is preventing me from seeing the simple > answer to this simple problem... > > irb-enhanced(main):001:0> str = "123&456" > => "123&456" > irb-enhanced(main):002:0> str.sub( /\&/, "\\&" ) > => "123&456" > irb-enhanced(main):003:0> str.sub( /\&/, "\\!" ) > => "123\\!456" > > This is because when the string is created \\& is converted into \&. In a substitution string \& refers to the part of the origional string which matches the regexp. > Why am I not able to escape the & in the string with an a \ ? If I do this: > > irb-enhanced(main):004:0> str.sub( /\&/, "\\\\&" ) > => "123\\&456" > Another way that works is to use the block form str.sub!( /\&/) {"\\&"} HTH -- Mark Sparshatt