Re: extracting numbers from a string
- From: come <come.news@xxxxxxx>
- Date: Tue, 12 Jun 2007 00:32:06 -0700
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]
On 12 juin, 08:45, Matt Jones <mattjonesph...@xxxxxxxxx> 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?
I expect it to involve the regex /\d+/, but I'm unclear how to extract a
portion of a string matching a regex.
Thank you
--
Posted viahttp://www.ruby-forum.com/.
.
- Follow-Ups:
- Re: extracting numbers from a string
- From: Robert Klemme
- Re: extracting numbers from a string
- References:
- extracting numbers from a string
- From: Matt Jones
- extracting numbers from a string
- Prev by Date: Re: How to get properties of a JSP page using IRB
- Next by Date: Re: "gem update --system" ignores where I installed rubygems
- Previous by thread: Re: extracting numbers from a string
- Next by thread: Re: extracting numbers from a string
- Index(es):
Relevant Pages
|