From: Bas van Gils Date: 2007-06-12T16:55:36+09:00 Subject: Re: extracting numbers from a string On Tue, Jun 12, 2007 at 03:45:04PM +0900, Matt Jones wrote: > I have filenames from various digital cameras: DSC_1234.jpg, > CRW1234.jpg, etc. What I really want is the numeric portion of that > filename. How would I extract just that portion? Some solutions have been posted already, but here's mine: irb(main):001:0> s="DSC_1234.jpg" => "DSC_1234.jpg" irb(main):002:0> s.sub(/\D+(\d+).*/,'\1') => "1234" basicially the regexp looks for : - one or more non-digits - one or more digits => because this is between parenthesis you can refer to it with \1 later on - something more The digits (safely stored in \1) is all you want to keep... this assumed you are only interested in the first sequence of numbers. Cheers Bas -- Bas van Gils , http://www.van-gils.org [[[ Thank you for not distributing my E-mail address ]]] Quod est inferius est sicut quod est superius, et quod est superius est sicut quod est inferius, ad perpetranda miracula rei unius.