shaezi has asked for the wisdom of the Perl Monks concerning the following question:
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 233394So 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: getting a number from a string
by davido (Cardinal) on Jan 05, 2004 at 04:10 UTC | |
by shaezi (Acolyte) on Jan 05, 2004 at 05:48 UTC | |
|
Re: getting a number from a string
by BrowserUk (Patriarch) on Jan 05, 2004 at 04:49 UTC | |
|
Re: getting a number from a string
by graff (Chancellor) on Jan 05, 2004 at 04:17 UTC | |
|
Re: getting a number from a string
by Roger (Parson) on Jan 05, 2004 at 05:33 UTC | |
|
Re: getting a number from a string
by jonadab (Parson) on Jan 05, 2004 at 11:50 UTC |