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

I need help validating three initials with no numbers or the underscore, upper or lowercase. I've tried:
if (($name =~ m/[a-zA-z]{3}/) && ($name =~ m/^\_{1,3}/)) { whatever; }
but neither seem to work. It will let three underscores go through but will not let three real initials go through (abc). Please help!

Edit kudra, 2002-04-15 Changed title

Replies are listed 'Best First'.
(jeffa) Re: validation
by jeffa (Bishop) on Mar 31, 2002 at 16:59 UTC

      /^[A-Za-z]{3}$/

      Use \z instead of $, as your regex also matches "foo\n".

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: validation
by CukiMnstr (Deacon) on Apr 01, 2002 at 00:31 UTC

    ($name =~ m/^\_{1,3}/)

    you are actually matching 1, 2 or 3 '_' here... and I believe you don't want that. If you want to match anything *but* 3 '_' you want

    ($name =~ m/^[^_]{3}$/)

    anyway, that is not what you are asking for, but I thought it would be useful... you might want to check perlre to clarify regexes and if you are relly interested in regexes, the book Mastering Regular Expressions from ORA is really good.

    just my $0.02