Hi,

was wondering what would be the best way to capture a number from a string that has both words and numbers.

The string is compoased of pairs of data that is a name (which could be more than one word) and a number associated with it, and each pair is delimited with a colon. For my example I'd like to capture only the first number from the first data pair in the string.

String looks like :

Happy Joy 002245:Dubloons 002256:hats 034523:paper clips 232344:pants 233394

So I just need to grab the first number 002245 from the string. I've tried splitting the data on a colon and then trying to capture the number but for some reason its not working. Any simple suggestions? Thanks!

sorry about not including what I was doing earlier....here's what I was attempting:

$string = Happy Joy 002245:Dubloons 002256:hats 034523:paper clips 232 +344:pants 233394; @items = split (/:/, $string); $number = split(/\d+/, $items[0]);

So instead of getting a value in $number I was getting just a "1" or "2". I couldn't figure out how I could use a regex just to grab what I needed. But I got davidos to work for me with: $number = $1 if $items[0]=~ m/(\d+)/;

Thanks guys!

Edited by Chady -- fixed formatting.


In reply to getting a number from a string by shaezi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.