Re: Regular expression

From: Date: Wed, 15 Feb 2006 10:40:11 +0000
Subject: Re: Regular expression
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-230428@lists.php.net to get a copy of this message
On 2/14/06, Patrick <pafo@pafo.net> wrote: > Hi, > > I am trying to validate a password, but havent figured out the pattern for > it yet. > The password must contain atleast 6 characters > a-zA-Z0-9_ > must start with a > a-zA-Z > and must have atleast one of the following characters > !#%&$£ As Curt said, it's probably better to do this programatically - if only so you can work out what it's doing six months down the road. However if you want to know to do it as a regexp: start with the letters a-z. ^[a-z] At least 5 legal characters following that (use a lookahead assertion to check): (?=[!#%&$£\w]{5}) Zero or more non-punctuation characters, followed by a a punctuation character, followed zero or more of any valid character. \w*[!#%&$£][!#%&$£\w]* And then anchor the end of the string with a $. Put all that together with a bit of case insensitivity and you get. $regexp = '/^[a-z](?=[!#%&$£\w]{5})\w*[!#%&$£][!#%&$£\w]*$/i'; $status = preg_match($regexp, $password) ? 'PASS' : 'FAIL'; -robin

« previous php.general (#230428) next »