Perl – Is a value decimal, real or a string?



To validate if a number is a number using regex.

[perl]

$number = “12.3”;

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

[/perl]