From: Lars Haugseth Date: 2009-08-05T19:05:13+09:00 Subject: Re: remove commas from string * 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." -- Lars Haugseth