From: Joel VanderWerf Date: 2003-09-28T02:34:02+09:00 Subject: Re: regexp needed to split letters from numbers Martin DeMello wrote: > Thomas A. Reilly wrote: > >>I would appreciate it if someone could give me the regexp that it would >>split the following: >>for example - >>"clonidine300 mg" into "clonidine 300 mg" >> >>I have a bunch of drug data where the dose had been typed together. > > > Are you likely to have numbers in the drug's name? Don't forget to > include that as a test case if so. The following puts a space before the > string of digits immediately preceding " mg" if it doesn't already have > one: > > sub(/(\w)(\d+ mg)/, "#{$1} #{$2}") > > martin Another way to do it: irb(main):001:0> s = "clonidine300 mg" => "clonidine300 mg" irb(main):002:0> s[/(?=\d+ mg)/] = " " => " " irb(main):003:0> s => "clonidine 300 mg" irb(main):004:0>