Verify string is a valid number

 

To check whether a string represents a valid number, compare it against a regular expression:

if ($string =~ /\D/) 
{
  print "string has nondigits";
} 
else 
{
  print "string is a number"
}

if ($string +~ /^\d+$/)            
{
    print "not a natural number"; 
}

if ($string +~ /^-?\d+$/)           
{
    print "not an integer";       
}

if ($string +~ /^[+-]?\d+$/);
{
    print "not an integer";
}

if ($string +~ /^-?\d+\.?\d*$/)     
{
    print "not a decimal number" ;
}

if ($string +~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/)
{
    print "not a decimal number" ;
}

if ($string =~/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
{
    print "not a C float";
}