in reply to Valid Numbers or decimals

I'm not a regex guru, but how about:
#!/usr/bin/perl my @numbers = (0, 3.14, 5 , "any strings can come here 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

Replies are listed 'Best First'.
Re^2: Valid Numbers or decimals
by eieio (Pilgrim) on May 06, 2005 at 11:54 UTC
    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.
      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