ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
2.1. Checking Whether a String Is a Valid NumberProblemYou want to check whether a string represents a valid number. This is a common problem when validating input, as in a CGI script. SolutionCompare it against a regular expression that matches the kinds of numbers you're interested in. if ($string =~ /PATTERN/) { # is a number } else { # is not } DiscussionThis problem gets to the heart of what we mean by a number. Even things that sound simple, like integer, make you think hard about what you will accept ("Is a leading + for positive numbers optional, mandatory, or forbidden?"). The many ways that floating-point numbers can be represented could overheat your brain. You must decide what you will and will not accept. Then, construct a regular expression to match those things alone. Here are some precooked solutions (the cookbook's equivalent of just-add-water meals) for most common cases. warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d+$/; # rejects -3 warn "not an integer" unless /^-?\d+$/; # rejects +3 warn "not an integer" unless /^[+-]?\d+$/; warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; These lines do not catch the IEEE notations of "Infinity" and "NaN", but unless you're worried that IEEE committee members will stop by your workplace and beat you over the head with copies of the relevant standards documents, you can probably forget about these strange numbers. If your number has leading or trailing whitespace, those patterns won't work. Either add the appropriate logic directly, or call the If you're on a POSIX system, Perl supports the sub getnum { use POSIX qw(strtod); my $str = shift; $str =~ s/^\s+//; $str =~ s/\s+$//; $! = 0; my($num, $unparsed) = strtod($str); if (($str eq '') || ($unparsed != 0) || $!) { return; } else { return $num; } } sub is_numeric { defined scalar &getnum } See AlsoThe regular expression syntax in perlre (1) and Chapter 2 of Programming Perl; your system's strtod (3) manpage; the documentation for the standard POSIX module (also in Chapter 7 of Programming Perl) |