doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I am trying the following regex to match the numbers after the decimal. If my input is -1.500, output is coming only as 5. Infact, it should be 500. Please help to correct my regex.

my $price = -1.500; if ( $price =~ m/^-\d+\.(\d*)$/g ) { print "matched $1 \n"; }

Replies are listed 'Best First'.
Re: regex to match numbers after decimal
by NetWallah (Canon) on Jan 15, 2014 at 05:35 UTC
    If you want it to behave like text, put it in quotes.
    $ perl -E 'my $price = "-1.500"; say for $price =~ m/^-\d+\.(\d*)$/g' 500

            If your eyes hurt after you drink coffee, you have to take the spoon out of the cup.
                  -Norm Crosby

Re: regex to match numbers after decimal
by Kenosis (Priest) on Jan 15, 2014 at 05:40 UTC

    Adding to NetWallah's suggestion, perl (the interpreter) 'sees' the numeric value of $price as -1.5, not the -1.500 you've scripted:

    perl -MO=Deparse,-p -e 'my $price = -1.500;' (my $price = (-1.5)); -e syntax OK