--- Timothy Kimball <[EMAIL PROTECTED]> wrote:
> 
> Stefan Kyrytow wrote:
> : As you can see the last two inputs should not be accepted. I am
> using $x =~ /[0-9]/ as my filter. I understand why the values are
> being accepted, I am searching for a number and 12bob & bob12 each
> contain a number.
> What I do not understand is how to search for just a number.
> 
> Nail the regex down to match at the beginning and end of the input
> string:
> 
> /^[0-9]+$/
> 
> or, equivalently, 
> 
> /^\d+$/
> 
> ^ matches the beginning of the string
> $ matches the end fo the string
> 
> If the string contains anything but digits, the match will fail.

Another option is to reverse your logic, and make sure it *doesn't*
have a *non*-digit. Better still is to do both.

 while(<STDIN>) {
    chomp; # get rid of the newline
    if (/\d/ and not /\D/) { # digits and not nondigits
        print "$_ is only numbers\n";
    } else {
        last if $_ eq "q";
        print "You must enter digits, and only digits.\n";
    }
 }


=====
print "Just another Perl Hacker\n"; # edited for readability =o)
=============================================================
Real friends are those whom, when you inconvenience them, are bothered less by it than 
you are. -- me. =o) 
=============================================================
"There are trivial truths and there are great Truths.
 The opposite of a trival truth is obviously false.
 The opposite of a great Truth is also true."  -- Neils Bohr

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

Reply via email to