From: "Erik Bågfors" Date: 2001-03-06T20:57:20+09:00 Subject: [ruby-talk:12118] Re: String.gsub() On 06 Mar 2001 20:50:24 +0900, Mike Bowler wrote: > The method String.gsub() isn't working the way I expected (or the way > the pickaxe book describes it for that matter). Given this code: > > def replaceSingleQuotesWithDouble( inputString ) > puts "original="+inputString > > puts inputString.gsub(/'(.+)'/, "\"\1\"") > end > > replaceSingleQuotesWithDouble("foo 'bar' foo") > > I would expect the output to be > > foo "bar" foo > > Instead I get > > foo "" bar > > The match is successful but it seems that the variable \1 hasn't been > assigned by the time we are doing the substitution. > > Am I doing something wrong? > I fell for that too. if you use " around the \1 you need to escape the \ with another \. so... change the gsub line to either puts inputString.gsub(/'(.+)'/, "\"\\1\"") or puts inputStrng.gsub(/'(+)'/, '"\1"') Where the second one is alot easier since you don't have to escape the " /Erik -- Erik B�gfors | http://erik.bagfors.nu/ erik@bagfors.nu | Erik.Bagfors@engohol.se Supporter of free software | GSM +46 733 279 273 fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32