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.
In reply to getting a number from a string by shaezi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |