Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Checking for single digit

by Enlil (Parson)
on May 06, 2003 at 01:23 UTC ( #255782=note: print w/replies, xml ) Need Help??


in reply to Checking for single digit

$input =~ /[0-9]/;
or
$input =~ /\d/;
checks to see if $input contains a digit anywhere in the string so that all of the following will match:
21 dfjdfkfjd2 jf2kjf 9343kfd2
if you want to insure that the string contains only one digit in it you can do something like the following (it will be true as long as only one thing is a digit no more no less):
$input =~ /^\D*\d\D*$/;
on the other hand if you want to make sure the only thing in $input is a single digit then the following will work:
$input =~ /^\d$/
which breaks down to finding the following pattern, the ^ anchors that the match starts at the start of the line, and the $ makes sure that the \d is the last thing in the line before the newline character(if it exists).

-enlil

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255782]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2018-05-24 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?