From: timsuth@... (Tim Sutherland) Date: 2002-07-13T16:39:25+09:00 Subject: Re: gsub() replacement containing replaced text In article <200207131507.17758.harryo@zip.com.au>, Harry Ohlsen wrote: >This feels like a terribly newbie question to be asking, but I'm stymied ... > >I want to replace all single quotes in a string with backslask followed by a >single quote. The obvious approach doesn't seem to work, presumably because >the replacement contains the pattern that's been replaced. > >I assume there's an easy way to do this, but can't for the life of me think >how. Here's an example of the problem ... > >irb(main):001:0> x="Don't say goodbye" >"Don't say goodbye" >irb(main):002:0> x >"Don't say goodbye" >irb(main):003:0> x.gsub("'", "\\'") >"Dont say goodbyet say goodbye" >irb(main):004:0> This issue is mentioned in 'Programming Ruby', "Standard Types" chapter (see http://www.rubycentral.com/book/). The problem is that you can use \&, \1 \2 etc. in the replacement string to match groups, so gsub is gobbling up your \\. The solution is to either do x.gsub("'", "\\\\'") or x.gsub("'") { "\\'" } The latter form works because when given a block, groups are given as arguments to the block instead of using \1, \2 etc, so gsub doesn't have to touch \'s in the replacement string. -- int main(){int l=0,$=0;char _[8]="To:";while(l-3?(_[l]=$++["#u,UTGK gpFyLY " #define p(r)p%:%:r("\45\56\52\163",(l=_[3])|7<:_:>%$,crypt(_,_+4%$)|_[6]%$); "7&nvXD(AqEhsZA`Oh@in #oI.ZX #pXXhe"]):l)++l%8||p(rintf)sorry:gnu_crypt_only:}