Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with a regular expression

Status
Not open for further replies.

Haazi2

Programmer
Aug 25, 2000
51
US
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 &quot;\$lngpswd = $lngpswd\n&quot;;

my $result = $lngpswd =~ /^$acode\w{5,16}/ ? &quot;OK&quot; : &quot;NO&quot;;
print &quot;\$result = $result\n&quot;;


if ( $result eq &quot;OK&quot; )
{
print &quot;$lngpswd is a good long password\n&quot;;
}
else
{
print &quot;$lngpswd is an invalid Long Password!\n&quot;;
}
 
i would use the regex to check for valid characters then make sure 16 > length($longpwd) > 5.
 
Try encapsulating the \w since it is two characters even if supposedly interpreted as one. Also, try adding the ending marker.

/^$acode(\w){5,16}$/
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Is this what you are looking for????
Code:
#!/usr/local/bin/perl

$acode = '1234';
$lngpswd = '1234wFkti_5k482';
$lngpswd =~ /^$acode[\d|\w|_]{1,12}$/;

print &quot;M - $&amp;\n&quot;;

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
 
GB, \w already contains [a-zA-Z0-9_]. Also, the {} only works on the previous match I believe, so you would match your set 1-12. That is not what we're looking for here. I think you would have to group it to do it that way, and then you would want 1-20. Anyway, it shouldn't matter what $acode is. Just put it in front and then match (\w){5,16}.

Tell me if my changes helped.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thanks tanderso and goBoating! I got it.

I used:

$result = $lngpswd =~ /^$acode(\w){1,12}$/ ? &quot;OK&quot; : &quot;NO&quot;;

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!
 
Tom,
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
 
Yeah, I see... I thought the part after the $acode was supposed to be 5-16 characters long, not including the $acode. I can see now that the whole thing is supposed to be that long.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top