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

I am fairly new to regular expressions. I am trying to write a regex that will prevent a user from picking a password that involves the phrases pass, password, admin, and administrator. The best I have come up with is \b(?!(\bpass(word)?\b)|(\badmin(istrator)?\b))\w+\b

However, this will not account for things such as "pass123" and previous attempts at correcting this regex have been unsuccessful. Any ideas to point me in the right direction?

Replies are listed 'Best First'.
Re: Regex Question
by diotalevi (Canon) on Jul 12, 2006 at 17:54 UTC
    /(pass|admin)/

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      or just /pass|admin/ if you don't need to know which word was used. Incidently, not using captures greatly speeds up the regexp.

        You're exageratting. It's just a password, not an epic. I would have said /(?:admin|pass)/ but figured I'd just go with the more readable (though slightly penalized) /(admin|pass)/.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Regex Question
by Ieronim (Friar) on Jul 12, 2006 at 18:17 UTC
    One of the most scalable ways:
    my @badwords = qw/pass admin qwerty/; #continue as you want if (grep { $pass =~ /$_/ } @badwords) { print "Bad password!" }

      Close.
      if (grep { $pass =~ /$_/ } map quotemeta, @badwords) {
      or
      if (grep { index($pass, $_) >= 0 } @badwords) {
      or
      my $re = join '|', map quotemeta, @badwords;
      if ($pass =~ /$re/) {

        Less scalable, as you cannot add regexes to @badwords ;)
      or even use index() instead of the regex....
      for @badwords { print "shame on you" if index $pass, $_ > -1; }
Re: Regex Question
by swampyankee (Parson) on Jul 12, 2006 at 21:20 UTC

    I'd also modify the regex to be case-insensitive:

    /pass|admin/i;

    corrected markup

    emc

    e(π√−1) = −1
Re: Regex Question
by Anonymous Monk on Jul 12, 2006 at 18:05 UTC
    Ok, thanks guys. Looks like I have a case of the trying-to-hards. :)