CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Complex matching?

 



ericsemail
Novice

Apr 19, 2007, 3:54 PM

Post #1 of 2 (2129 views)
Complex matching? Can't Post

Hi- I'm trying to force a password string to require special characters: must contain at least 1 uppercase letter, 1 lowercase letter, and one non-alphanumeric character. Unfortunately, this must be done using ONLY a one line regular expression--it's for a configuration script that gets read by our Application--so unfortunately, I'm trying to construct a one line regexp that will do this.... I'm not having much luck.

my $pw = 'testThisPw!'; or my $pw = '%testThis!PW'; both of these should pass the regexp test, while: my $pw = 'testingThis' would fail because it doesn't have a non-alphanumeric character...

In any event, I keep confusing myself when trying to attempt to construct a regexp that satisfies all my needs. Is there a "AND" operator in a perl regexp like the "OR" ( | )? Is this even possible to accomplish in a one line regexp? Thanks for the help in advance.


ericsemail
Novice

Apr 20, 2007, 9:35 AM

Post #2 of 2 (2116 views)
Re: [ericsemail] Complex matching? [In reply to] Can't Post

Got it:


Code
 
#!/usr/bin/perl

use strict;
use Test::More qw(no_plan);

my @good = qw(@asdfERER dfdfddf$$$TTTTT TTT$$ddd ddTT$$ Ax?xxxxxxxxxx Ax>);
my @bad = qw(testuser1 asdfasdf@ TERERE%^ asdfasdf ERERERE 23344334 asdfasdfERE\
ERER *&^^%$%$^);

my $re = '^(?:[A-Z].*[a-z].*\W.*|
[A-Z].*\W.*[a-z].*|
[a-z].*\W.*[A-Z].*|
[a-z].*[A-Z].*\W.*|
\W.*[a-z].*[A-Z].*|
\W.*[A-Z].*[a-z].*)$';


ok (/$re/ox, "good: $_") for @good;
ok (!/$re/ox, "bad: $_") for @bad;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives