On Dec 6, Daniel Falkenberg said: >What if $string does not meet the criteria in s/[^a-zA-Z0-9]+//g; can I >get a print statement to say... > >if ($string ne s/[^a-zA-Z0-9]+//g) { > print "$string does not meet our criteria...! Please try another >password\n"; >}
You mean, "how can I see if a string contains invalid characters?" This is an exercise from my book (chapter 2, "Simple Pattern Elements): ========================================================================== 8. Write a simple function that determines if the value given to it is a valid hexadecimal number. Hexadecimal numbers have digits from 0 to 9, and then from "A" to "F" (in upper- or lower-case). Does your function accept the empty string has a hexadecimal number? If so, fix it so that it does not. ========================================================================== And here is the solution given: ========================================================================== 8. Here's the first approach, which ends up matching the empty string: # if $num does NOT contain a # character other than a-f, 0-9 # it is an OK hex number if ($num !~ /[^a-f0-9]/i) { ... } The reason that accepts an empty string is because an empty string doesn't contain a character outside of the required character set. That being said, there are many ways to make sure our string is not empty: # here, ... represents our regex from above # check for length if (length($num) and ...) { ok } # check against '' if ($num ne '' and ...) { ok } # match a valid character if ($num =~ /[a-f0-9]/i and ...) { ok } ========================================================================== I trust you can extrapolate that to your case. You want to make sure a string contains ONLY the characters [A-Za-z0-9]. So use that in these code examples instead of the hexadecimal class, [a-fA-F0-9]. You might notice I've used [a-f0-9] in these examples -- that's because I have the /i modifier on, which means the regex is case-insensitive. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]