From: dblack@... Date: 2003-05-30T07:04:35+09:00 Subject: Re: substitution weirdness Hi -- On Fri, 30 May 2003, Ian Macdonald wrote: > Hi, > > Here's some behaviour I don't immediately understand: > > irb(main):119:0> foo = "'123" > => "'123" > irb(main):120:0> foo.gsub(/'/, "\\'") > => "123123" > > Why does the apostrophe get replaced with a copy of the text that > follows it? What you're hitting is a special meaning of \' -- namely, the post-match part of the match. It's similar to the $`, $&, $' variables (pre-match, match, post-match). > I'm trying to replace every instance of ' with \' but it's proving very > tricky. Nothing I can come up with seems to work, no matter how many > backslashes I use, whether I use single or double quotes, whether or not > I use gsub with a block, etc. Try this: "'123".gsub(/'/, "\\\\'") David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav