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

This node falls below the community's minimum standard of quality and will not be displayed.
  • Comment on Matching alphabetic chars without underscore

Replies are listed 'Best First'.
Re: Matching alphabetic chars without underscore
by Corion (Patriarch) on Feb 16, 2006 at 15:26 UTC

    So, when's your homework due?

    Have you read perlre ? Did you understand the section about character classes?

Re: Matching alphabetic chars without underscore
by ikegami (Patriarch) on Feb 16, 2006 at 15:45 UTC

    One way to "allow only" is to check for the characters which are not allowed. This can be done by checking the inverse set of the allowed characters. [^...] will do that.

    if ($str =~ /[^[:alpha:][:digit:].]/) { die("Bad data\n"); }

    The other way is to use [...] to check for allowed characters. But you need to make sure every character in the string is checked by anchoring the regexp with ^ and \z.

    if (!($str =~ /^[[:alpha:][:digit:].]*\z/)) { die("Bad data\n"); }

    Everything used here is documented in perlre.

    Update: Switched to character classes. Original code in HTML comment.