Re: extracting numbers from a string



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/.


.



Relevant Pages

  • Split Single Address Field into Component Parts
    ... would make a copy of the database before making any ... would subtract that part of the string from the working ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Split Single Address Field into Component Parts
    ... >address field using an update query. ... >string functions. ... I ran an update query to extract the portion of the ...
    (microsoft.public.access.queries)
  • Re: Regex question
    ... structure of the date you're trying to extract. ... For example, in Regex you can ... pattern that will ensure a valid date within the range allowed by T-SQL ... valid date from a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: regular expression question
    ... I am a newbie to regular expressions and want to extract a number from the ... end of a string within an HTML document. ... A really nice free regex editor is called expresso. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Regex question
    ... If you want to pull a valid date from a string, then you have to write a pattern that defines the lexical structure of the date you're trying to extract. ... What you're trying to do is simple by Regex standards, but still requires a lot more specificity than "a digit or a slash". ...
    (microsoft.public.dotnet.languages.csharp)