vishi has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm trying to do some input validation and I'm pretty sure I'm doing something really stupid that's making it not work the way I want to.

I want to accept values from users that are of the form:

SB0 SB00

So, basically, it is a string "SB" or "sb" (case insensitive)followed by either 1 or two digits. Everything else should fail, and prompt the user to enter the right string. Here's the code I have right now. Not sure where I am going wrong:

our $sbNumHost1=''; while ($sbNumHost1 eq "") { print "Enter the SB## \t: "; chomp($sbNumHost1 = ReadLine(0)); if ($sbNumHost1 !~ m/(SB)(\d{1,2})/i) { print "matched >>>>>>>$&<<<<<<<\n"; $sbNumHost1 = ''; } } print "done\n";

Unfortunately, this code gladly accepts inputs like SB111 which should not be a valid input.

Helllllllppppuh

Replies are listed 'Best First'.
Re: Noob Question - not sure what's wrong
by ikegami (Patriarch) on May 11, 2011 at 07:02 UTC

    "SB111" does indeed match the pattern. You want /^SB[0-9]{1,2}\z/i.

    Then there's the issue that you use $& when no match occur, even though it's only set on match. I think you use !~ when you meant to use =~.

    Then there's the issue that you use $&, which causes all non-capturing regex matches in your program to become slower. $sbNumHost1 would be a suitable replacement for $& here.

      Thanks ikegami,
      But I think I found what I was doing wrong. I simplified the code to this and it now works...

      while ($sbNumHost1 !~ m/^(SB)(\d{1,2})$/i) { print "Enter the SB## \t: "; chomp($sbNumHost1 = ReadLine(0)); } print "done\n";

        That matches "SB00\n" and you said it shouldn't. Replace /$/ with /\z/ to get the stated desired behaviour.

        \d can match more than 0-9. I presume you don't want to match arab digits.

        That uses needless captures. Get rid of them.

        m/^(SB)(\d{1,2})$/i
        should be
        m/^SB[0-9]{1,2}\z/i