in reply to Re: Valid Numbers or decimals
in thread Valid Numbers or decimals

If the OP want to treat a number that ends with a decimal point (e.g., 7.) as valid, the above regular expression needs to be tweaked. Something like  grep { /^\d*\.?\d+$/ || /^\d+\.?$/ } should work.

Replies are listed 'Best First'.
Re^3: Valid Numbers or decimals
by reasonablekeith (Deacon) on May 06, 2005 at 12:38 UTC
    neither of these cope with the fact that...
    my $num = 1_000_000;
    ...is a perfectly valid number declararion. I think what the OP is after is way to get at what Perl is doing when it compains after you do something like this...
    my $num = 1_000_000; if ($num == "ssf") { print "uh oh"; }
    Re-writing an existing Perl check in a regex is best avoided if possible IMHO
    ---
    my name's not Keith, and I'm not reasonable.
      How about:
      #!/usr/bin/perl my @numbers = (0, 3.14, 5 , 4_000_000, 3_000.42, "any strings 234 "); print "@numbers\n"; my @numgoods = grep{/^(\d*[\._]?\d+)$/ } @numbers; print "@numgoods\n";

      I'm not really a human, but I play one on earth. flash japh