From: "RĂ¼diger Bahns" Date: 2009-08-06T22:55:07+09:00 Subject: Re: remove commas from string s.ross schrieb: ... >>> $ 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? This would make 2344,667 from 2,344,667 (leave the second comma). (Why the "\b"s and not just /(\d),(\d{3})/ ?) To clean up sums like those that are made as gifts for banks in this times (like 1,000,000,000,000) in 1.8 you need something like s.gsub /\d(,\d{3})+/ do |mo|; mo.gsub ',',''; end R.