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

I need to determine if this variable has at least one number and at least one character. I have the following regular expression but how do I get it to fail if both of my conditions are not met?
$password_tainted =~ /([-\\\/\!\*\+\@\s\w\$\.\{6,10})/
Any thoughts would be greatly appreciated.

Replies are listed 'Best First'.
Re: problems with regular expressions
by sauoq (Abbot) on Jun 11, 2003 at 20:55 UTC
    I need to determine if this variable has at least one number and at least one character.

    I assume you mean "and at least one letter." It'll have one character if its length is greater than 0.

    fail() unless $password =~ /\d/; fail() unless $password =~ /[A-Za-z]/;

    By the way, that regular expression is a bit of a mess. First, there seems to have been a transcription error because you never close your character class. Ignoring that though, there's just no reason to escape all of those characters and doing so just makes it difficult to read.

    $password =~ /([-\\\/!*+@\s\w$.]/; # This is fine.

    Any thoughts would be greatly appreciated.

    I'm not a big fan of restricting passwords. That said, I know education isn't always an option. You can usually be sure, though, that if you require that a password has one letter and one number, 70% are going to choose a password of all letters and slap a number on the end. Another 20% will put the number first. So, the real effect, with such simple rules, is actually to reduce the work that a cracker would have to do if he got ahold of your encrypted passwords. You might be better off if you insist that one character other than the first or the last is a non-letter.

    fail() unless $password =~ /^.+[^A-Za-z]./;

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: problems with regular expressions
by cchampion (Curate) on Jun 11, 2003 at 20:33 UTC