From: "Wesley J. Landaker" Date: 2003-07-23T02:16:32+09:00 Subject: Re: String substitution without RegEx Apparently, ts recently wrote: >>>>>> "A" == Andreas Schwarz writes: > > A> I would still need a function to remove backreferences from the A> > replacement string. It's possible, of course, but it hurts me that I A> > have to use such "hacks" for a simple substitution. > > Perhaps with Regexp#escape > > svg% ruby -e 'p "[]{}".gsub(/#{Regexp.escape("]{")}/, > Regexp.escape("\\1"))' "[\\1}" > svg% And then, so you don't have to type that all every time, just make it a method of String. Being able to do this is one of the best features of Ruby. =) class String def simple_sub(from,to) sub(/#{Regexp.escape(from)}/,Regexp.escape(to)) end end Wes