Update: Caution: The code below
does not work as advertised! Unfortunately, it was tested only with passwords (I assume the intent of the code is to validate candidate passwords) that met
all four of the four optional conditions. Had I tested a password meeting only
three of the optional conditions, I would have seen that it was rejected as invalid. Apparently, the
{3,} quantifier at the end of the non-capturing group in the
$valid regex is simply ignored after the warning is issued and Perl just soldiers on. Nobody has called me on this, so I figure I had better 'fess up; I would not want anyone to stumble across this post in the archives someday and be misled onto the path of error.
I hope they don't make me give back the XP.
Original Post:
Other than the warning (sorry for the line-wrap munging on this), the following approach seems to work. Except for explicitly suppressing the warning, can anyone think of a way to make this work 'quietly'?
>perl -wMstrict -le
"my $min_10 = qr{ (?= .{10}) }xms;
my $a_digit = qr{ (?= .* \d) }xms;
my $a_lower = qr{ (?= .* [[:lower:]]) }xms;
my $an_upper = qr{ (?= .* [[:upper:]]) }xms;
my $special = qr{ [@\#\$%^&+=] }xms;
my $a_spec = qr{ (?= .* $special) }xms;
my $valid = qr{
\A $min_10 (?: $a_digit $a_lower $an_upper $a_spec){3,}
}xms;
while (chomp(my $pw = <STDIN>)) {
my $ok = $pw =~ $valid;
print qq{'$pw' }, $ok ? 'valid' : 'INVALID';
}
"
(?: (?msx-i: (?= .* \d) ) (?msx-i: (?= .* [[:lower:]]) ) (?ms
+x-i: (?= .
* [[:upper:]]) ) (?msx-i: (?= .* (?msx-i: [@\#\$%^&+=] )) ))
+{3,}
matches null string many times in regex; marked by
+ <-- HERE
in m/
+ \A (?msx
-i: (?= .{10}) ) (?: (?msx-i: (?= .* \d) ) (?msx-i:
+(?= .* [[:
lower:]]) ) (?msx-i: (?= .* [[:upper:]]) ) (?msx-i: (?= .* (?msx-i: [@
+\#\$%^&+=]
)) )){3,} <-- HERE / at -e line 1.
'' INVALID
' ' INVALID
x
'x' INVALID
xxxxxxxxx
'xxxxxxxxx' INVALID
xxxxxxxxxx
'xxxxxxxxxx' INVALID
%Aa123456
'%Aa123456' INVALID
%Aa1234567
'%Aa1234567' valid
1aA%23456
'1aA%23456' INVALID
1aA%234567
'1aA%234567' valid
Terminating on signal SIGINT(2)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.