×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Problem with a regular expression

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";   
   }

RE: Problem with a regular expression

i would use the regex to check for valid characters then make sure 16 > length($longpwd) > 5.

RE: Problem with a regular expression

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.
http://www.oac-design.com

RE: Problem with a regular expression

Is this what you are looking for????

#!/usr/local/bin/perl

$acode = '1234';
$lngpswd = '1234wFkti_5k482';
$lngpswd =~ /^$acode&#91;\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

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.
http://www.oac-design.com

RE: Problem with a regular expression

(OP)
Thanks tanderso and goBoating! I got it.

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

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

RE: Problem with a regular expression

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.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close