develooper Front page | perl.beginners | Postings from February 2002

Re: Allow only letters and numbers?

Thread Previous
Jeff 'japhy' Pinyan
February 21, 2002 16:58
Re: Allow only letters and numbers?
Message ID:
On Feb 22, Daniel Falkenberg said:

>The code below of coarse will count all characters in the string $string
>$string = "4556jhkl";
>$count = $string =~ tr/a-zA-Z0-9//;
>How would I go about only allowing numbers and letters of the alpahbet?

You could say:

  if ($string =~ tr/a-zA-Z0-9//c) {
    print "bad character found";

The /c modifier means "invert the set", so that tr/aeiou//c scans for
characters that are NOT a, e, i, o, or u.

If a regex is want you want to use, I'd suggest:

  if ($string =~ /[^a-zA-Z0-9]/) {
    # bad character found

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About