LINUX.ORG.RU

use Data::Validate wq(:math);
  
if(defined(is_integer($suspect))){
     print "Looks like an integer\n";
}
  
my $name = is_alphanumeric($suspect);
if(defined($name)){
      print "$name is alphanumeric, and has been untainted\n";
} else {
      print "$suspect was not alphanumeric"
}
  
# or as an object
my $v = Data::Validate->new();
  
die "'foo' is not an integer" unless defined($v->is_integer('foo'));

или напиши сам на regexp.

swop
()

#!/usr/bin/perl -w
# number.pl
$text = shift ;
print $text ;
print " -- this is a number\n" if ( $text =~ /^[+-]?\d+(\.\d*|)?([eE][+-]?\d+)?$/ );

типа того...

$ ./number.pl "-123.45e-56"

-123.45e-56 -- this is a number

anonymous
()
Ответ на: комментарий от vahvarh

> $number=~/^(?:\d+|\d*\.\d+|\d+\.\d*)(?:[eE](?:\d+|\d*\.\d+|\d+\.\d*))$/

а [+-] после [Ee] ? :)

anonymous
()


А что, документацию отменили?
Читайте FAQ:

$ perldoc -q integer

How do I determine whether a scalar is a number/whole/integer/float?

Assuming that you don’t care about IEEE notations like "NaN" or
"Infinity", you probably just want to use a regular expression.

if (/\D/) { print "has nondigits\n" }
if (/^\d+$/) { print "is a whole number\n" }
if (/^‐?\d+$/) { print "is an integer\n" }
if (/^[+‐]?\d+$/) { print "is a +/‐ integer\n" }
if (/^‐?\d+\.?\d*$/) { print "is a real number\n" }
if (/^‐?(?:\d+(?:\.\d*)?│\.\d+)$/) { print "is a decimal number\n" }
if (/^([+‐]?)(?=\d│\.\d)\d*(\.\d*)?([Ee]([+‐]?\d+))?$/)
{ print "a C float\n" }

There are also some commonly used modules for the task.
Scalar::Util (distributed with 5.8) provides access to perl’s
internal function "looks_like_number" for determining whether a
variable looks like a number. Data::Types exports functions that
validate data types using both the above and other regular
expressions. Thirdly, there is "Regexp::Common" which has regular
expressions to match various types of numbers.
Those three modules are available from the CPAN.

If you’re on a POSIX system, Perl supports the "POSIX::strtod"
function. Its semantics are somewhat cumbersome, so here’s a
"getnum" wrapper function for more convenient access. This
function takes a string and returns the number it found, or "undef"
for input that isn’t a C float.
The "is_numeric" function is a front end to "getnum" if you just
want to say, ‘‘Is this a float?’ The "is_numeric" function is
a front end to "getnum" if you just want to say, ‘‘Is this a float?’

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 undef;
} else {
return $num;
}
}

sub is_numeric { defined getnum($_[0]) }

Or you could check out the String::Scanf module on the CPAN
instead. The POSIX module (part of the standard Perl distribution)
provides the "strtod" and "strtol" for converting strings to double
and longs, respectively.

anonymous
()

вот так это делается:

#!/usr/bin/perl

$_=qq~
1234
34 -4567
 3456
-0.35e-0,2
    56grf45
-.034 E20
     -.034 e2,01   -,045 e-,23
  -,034 e201  3e-.20
-,045 e-,23 e-0.88

4 E-0.20
22 
E-21
 -0.2 w         4 3
345
2 ^-,3
~;

$a='[+-]?\d*[,.]?\d+';print"$_\n"for/$a\s?[e^]$a|$a|[+-]?e$a/ig

найдет и выделит все числа

vilfred ☆☆
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.