From: Robert Klemme Date: 2007-06-12T20:05:08+09:00 Subject: Re: extracting numbers from a string On 12.06.2007 09:32, come wrote: > If you just want to extract one number from a string, you could write > something like : > > if a="DSC_1234.jpg" > > then a[/\d+/] will give you the first longest string of numbers, so > 1234. > > If you want to be more precise, you could use parenthesis to extract > the exact portion you want, like : > > a[/DSC_(\d+)\.jpg/,1] (<=> a.match(/DSC_(\d+)\.jpg/)[1]) > > or even : a[/\ADSC_(\d+)\.jpg\Z/,1] Or even simpler irb(main):001:0> "DSC_1234.jpg"[/\d+/] => "1234" irb(main):002:0> Integer("DSC_1234.jpg"[/\d+/]) => 1234 Kind regards robert