Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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]/;
$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).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255782]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2018-08-21 14:25 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (200 votes). Check out past polls.