This solution might not be optimal (as it is not very strict and not exactly what you want) but you could also use pack and unpack.

As you can see from the code below, I tested the eval, the regex posted by japhy, and two versions of pack,
one that stores the number as a double precision float (packstuffD), and one that stores the number as a single precision float (packstuffF).

The regex outputs:
4.34 -2.33333 5 20
Whereas both of the packs output:
4.34 -2.33333 0 5 0 5 12 0 0 0 20 4.3e+015

As you can see, anything that is not a number gets turned into a zero by the pack/unpack process, which is good
and harmless, and only returns valid Perl numbers which is not quite what you wanted
so this became was a fun little exercise for myself!
use Benchmark; sub regexstuff() { map {$foo = $_ if /^-?(?=\d|\.\d)\d*\.?\d+$/} (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffD() { map { $foo = unpack ("d", pack ("d", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffF() { map { $foo = unpack ("f", pack ("f", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub evalstuff() { map { eval{ local $^W = 1; $_ + 0 } } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); if ( $@ ) { $result = "$invalue is *not* a Number; Details: $@" } else { $result = "$invalue appears to be a number."; } } timethese ( 100000, { packstuffD => "packstuffD", regex => "regexstuff", eval => "evalstuff", packstuffF => "packstuffF" } ); eval: 13 wallclock secs (11.97 usr + 0.02 sys = 11.99 CPU) @ 83 +42.37/s (n=100000) packstuffD: 11 wallclock secs (10.06 usr + 0.00 sys = 10.06 CPU) @ 99 +45.30/s (n=100000) packstuffF: 10 wallclock secs (10.63 usr + 0.02 sys = 10.65 CPU) @ 93 +93.20/s (n=100000) regex: 11 wallclock secs (11.01 usr + 0.01 sys = 11.02 CPU) @ 90 +77.71/s (n=100000)

In reply to Re: (Buzzcutbuddha - Pack is an alternative) Validating Numbers in CGI Script? by buzzcutbuddha
in thread Validating Numbers in CGI Script? by footpad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.