in reply to Stupid regex question - numerics

The reason is that you're using a character class which says "match at least 1 of anything in the class" so any string with a dot, a digit or a '-' sign in it will match be (incorrectly) satisfied in the in the if ...

I suspect you are after something a little more forceful:
$long !~ /^([\-]*[\d]+[\.]*[\d]*)$/ (character classes are only left in for improving readability...)

This regex says: a leading "-" sign is optional, but must then have at least 1 digit, followed by an optional dot (to cater for cases where $long might equal an integer or 0) followed by optional digits...

Replies are listed 'Best First'.
Re^2: Stupid regex question - numerics
by JavaFan (Canon) on Mar 01, 2010 at 14:52 UTC
    Useless use of square brackets detected! Useless use of backslash detected! Exactly zero of the 8 square brackets in your regexp are needed.1 /^([\-]*[\d]+[\.]*[\d]*)$/ is identical to /^(-*\d+\.*\d*)$/. Furthermore, your regexp also matches -----------------------1.................................. It's doubtful the OP wants to match that. I think you want to use ? in all but one of the cases you wrote *.

    Also note that your regexp doesn't match .123. But it does match ٦৬६.

    1The square brackets don't add anything to the readability. Furthermore, pre-5.10, character classes consisting of a single character are significantly slower than using said character directly (many optimizations won't happen if there's a character class).

Re^2: Stupid regex question - numerics
by ultranerds (Hermit) on Mar 01, 2010 at 12:01 UTC
    Ah , you legend! Works a charm - thanks :)