$ perl -E' print "Enter a number or string: "; my $s = <STDIN>; chomp $s; if ( $s =~ /^[0-9,.E]+$/ ) { say "$s is a number\n"; } else { say "$s may be a string\n"; } ' Enter a number or string: ,,, ,,, is a number $ perl -E' print "Enter a number or string: "; my $s = <STDIN>; chomp $s; if ( $s =~ /^[0-9,.E]+$/ ) { say "$s is a number\n"; } else { say "$s may be a string\n"; } ' Enter a number or string: EEE EEE is a number $ perl -E' print "Enter a number or string: "; my $s = <STDIN>; chomp $s; if ( $s =~ /^[0-9,.E]+$/ ) { say "$s is a number\n"; } else { say "$s may be a string\n"; } ' Enter a number or string: -777 -777 may be a string
If you are going to use a regular expression then you should probably read the FAQ: How do I determine whether a scalar is a number/whole/integer/float?
In reply to Re^2: A function to determine if a string is numeric
by jwkrahn
in thread A function to determine if a string is numeric
by juliosergio
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |