Problem with a regular expression
Problem with a regular expression
(OP)
I'm having a problem trying to get a regular expression to
work for checking a long password. The long password consist
of an access code plus any combination of numbers, upper or
lower case letters, or the underscore(_). The total length of the long password must be at least 5 characters and cannot exceed 16 characters. The regular expression I use below doesn't seem to work. Can someone tell me what I'm doing wrong. Knowing me it's probably something simple. Thanks in advance for your help.
#!/usr/local/bin/perl -w
use strict; # enforce declarations and quoting
use diagnostics; # hints for errors
my $acode = "1234";
print "Type in your long password\n";
my $lngpswd = <STDIN>;
chomp($lngpswd);
print "\$lngpswd = $lngpswd\n";
my $result = $lngpswd =~ /^$acode\w{5,16}/ ? "OK" : "NO";
print "\$result = $result\n";
if ( $result eq "OK" )
{
print "$lngpswd is a good long password\n";
}
else
{
print "$lngpswd is an invalid Long Password!\n";
}
work for checking a long password. The long password consist
of an access code plus any combination of numbers, upper or
lower case letters, or the underscore(_). The total length of the long password must be at least 5 characters and cannot exceed 16 characters. The regular expression I use below doesn't seem to work. Can someone tell me what I'm doing wrong. Knowing me it's probably something simple. Thanks in advance for your help.
#!/usr/local/bin/perl -w
use strict; # enforce declarations and quoting
use diagnostics; # hints for errors
my $acode = "1234";
print "Type in your long password\n";
my $lngpswd = <STDIN>;
chomp($lngpswd);
print "\$lngpswd = $lngpswd\n";
my $result = $lngpswd =~ /^$acode\w{5,16}/ ? "OK" : "NO";
print "\$result = $result\n";
if ( $result eq "OK" )
{
print "$lngpswd is a good long password\n";
}
else
{
print "$lngpswd is an invalid Long Password!\n";
}
RE: Problem with a regular expression
RE: Problem with a regular expression
/^$acode(\w){5,16}$/
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: Problem with a regular expression
#!/usr/local/bin/perl
$acode = '1234';
$lngpswd = '1234wFkti_5k482';
$lngpswd =~ /^$acode[\d|\w|_]{1,12}$/;
print "M - $&\n";
This assumes the access code is always four chars long. Otherwise the {1,12} will be variable instead of static.
/^$acode[\d|\w|_]{1,12}$/
$ - pins the match on the end of the string
{1,12} - find between 1 and 12 of the members of the class
[\d|\w|_] - a class of chars including digits or word chars or underscore
^ - pins the match to the start of the string
'hope this helps...
keep the rudder amid ship and beware the odd typo
RE: Problem with a regular expression
Tell me if my changes helped.
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: Problem with a regular expression
I used:
$result = $lngpswd =~ /^$acode(\w){1,12}$/ ? "OK" : "NO";
Which makes sure the long password is no more than 16 characters. The $ forces the the number of characters
to match at the end of the string. The matching at the end
of the string was the key. I knew it had to be simple. Thanks again!
RE: Problem with a regular expression
thanks for the critique. You're obviously correct about the \w. My code was redundant, redundant. Another moment of attention would have prevented that.
I do think the {1,12} is appropriately used, however. I intended it to apply to the char class described in [].
Thanks again.
keep the rudder amid ship and beware the odd typo
RE: Problem with a regular expression
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com