From: "s.ross" Date: 2009-08-06T15:04:13+09:00 Subject: Re: remove commas from string On Aug 5, 2009, at 3:55 AM, Robert Klemme wrote: > 2009/8/5 Lars Haugseth : >> * Jason Lillywhite wrote: >>> >>> I have following string: >>> >>> s = "B747-400, 8,357 miles, 561 mph, 4 Pratt & Whitney PW 4056 >>> turbofans, 56,000 lbs." >>> >>> I want to remove the comma only from the numbers (8,357 miles and >>> 56,000 >>> lbs) separating the thousands. I want the string to read as follows: >>> >>> "B747-400, 8357 miles, 561 mph, 4 Pratt & Whitney PW 4056 turbofans, >>> 56000 lbs." >> >> Ruby 1.9 supports look-behind in regular expressions (Ruby 1.8 >> only supports look-ahead): >> >> $ irb1.9 >> >> irb(main):001:0> s = "B747-400, 8,357 miles, 561 mph, 56,000 lbs." >> => "B747-400, 8,357 miles, 561 mph, 56,000 lbs." >> >> irb(main):002:0> s.gsub(/(?<=\d),(?=\d)/, '') >> => "B747-400, 8357 miles, 561 mph, 56000 lbs." > > I'r rather do this to be a bit more robust: > > irb(main):003:0> s.gsub(/(?<=\d),(?=\d{3})/, '') > => "B747-400, 8357 miles, 561 mph, 56000 lbs." > > Kind regards > > robert Why so complex? Perhaps: >> s.gsub(/\b(\d+),(\d+)\b/, '\1\2') => "B747-400, 8357 miles, 561 mph, 56000 lbs." Is there a corner case I'm missing here?