in reply to Re^3: need help with a Regex
in thread need help with a Regex

Well, numbers fall into so many possible formats that it's impossible to catch them all. I'm going to assume that any integer or decimal format is allowed, and write the following:
foreach (<DATA>) { print $_ if $_ =~ /^(\?|-?\d*\.?\d+)?$/; } __DATA__ ? 3 3.3 .3 -3 -3.3 -.3 ? 3-3 -

Replies are listed 'Best First'.
Re^5: need help with a Regex
by Limbic~Region (Chancellor) on Oct 07, 2004 at 12:16 UTC
    TedPride,
    That is why I suggested Scalar::Util's 'looks_like_number'. For instance, your regex misses at least numbers in scientific notation. Here is the applicable code:
    # Copied from Scalar::Util sub looks_like_number { local $_ = shift; # checks from perlfaq4 return $] < 5.009002 unless defined; return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # +a C float return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006 +001 and /^Inf$/i); 0; }

    Cheers - L~R