in reply to Re^2: Extraction number from Text
in thread Extraction number from Text

Great point with respect to the '*' quantifier for the character class.

The OP's example, which uses the '*' quantifier would, of course, allow NAN's to be parsed as numbers. For example: "--eeeeeeeeeee1" would be accepted as a number when it's definitely not (although perl could evaluate that string in numeric context giving it a value of 1). I didn't attempt to address that issue, but it goes to punctuate your next point which is.....

Regex::Common is a nice resource too. If there's a resource that knows how to parse numbers, why write ones own number parser when it (a) takes more time, and (b) possibly introduces bugs? Regex::Common is the answer to both 'a' and 'b'.


Dave

Replies are listed 'Best First'.
Re^4: Extraction number from Text
by JavaFan (Canon) on Jun 14, 2010 at 10:08 UTC
    For example: "--eeeeeeeeeee1" would be accepted as a number when it's definitely not (although perl could evaluate that string in numeric context giving it a value of 1).
    Eh, no. "--eeeeeeeeeee1" in numerical context is 0. When casting a string to a numeric value, Perl will always look at the beginning of a string. Something that starts with "--" cannot be a number; hence, it'll get the value 0.
    $ perl -wE 'say 0 + "--eeeeeeeeeee1"' Argument "--eeeeeeeeeee1" isn't numeric in addition (+) at -e line 1. 0 $
Re^4: Extraction number from Text
by proceng (Scribe) on Jun 14, 2010 at 13:51 UTC
    Regex::Common is a nice resource too.
    s/Regex/Regexp/... ;-)