From: Robert Klemme Date: 2009-08-05T19:55:10+09:00 Subject: Re: remove commas from string 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/