in reply to Extracting a number from a string

my ($num) = $string =~ /(\d+)/; # Added $num += 0;

$num is in parens to put the right side into list context, which makes match return the value matched.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Extracting a number from a string
by queue (Beadle) on Jan 12, 2003 at 17:13 UTC
    That would make "05 stringstring" return 05, though, not 5.

    Maybe something like:

    my ($num) = $string =~ /0*(\d+)/;

    That would also correctly set $num to 0 for a string like "string0string", right?


    Queue
    still making his way through Mastering Regular Expressions