Page 1 of 1
Trouble With Regex
Posted: Mon Nov 20, 2006 7:24 pm
by Superman859
I'm just starting to learn Regex and am having trouble coming up with an expression.
Basically I want a to verify a password matches criteria. It needs to be at least 6 characters long, have at least 1 uppercase, 1 lowercase, and 1 number. That is all the criteria.
I realize I could put this together very easily without using regex at all, but since I am beginning to study regex, I want to try and write it out in regex.
I tried what is below, but it doesn't seem to work. It simply returns a match if the password is 6 characters or longer, but isn't requiring at least 1 of each. I'm sure it's a simple problem but I'm just starting with regex.
[[a-z]+[A-Z]+[0-9]+]{6,}
Posted: Tue Nov 21, 2006 10:40 am
by GeertDD
Hmm, interesting question. I'm learning regular expressions myself as well and can't think of a one-line solution for now.
You could of course apply different regexes to make sure the password matches all the criteria.
Code: Select all
function password_is_safe($str) {
return (
preg_match('/[A-Z]/', $str)
&& preg_match('/[a-z]/', $str)
&& preg_match('/[0-9]/', $str)
&& preg_match('/^.{6,}$/', $str)
);
}
However, this is not an efficient setup. If you do it this way, you'd better use faster functions like strlen() and strpos(), as you probably already realized. I just couldn't think of a single regex. I'll be watching this topic.
Posted: Tue Nov 21, 2006 12:20 pm
by John Cartwright
alphanumeric characters between 6 and 25 chars.
although for this you won't need regex, simply ctype_alpha() and strlen() would do the trick and is faster.
Code: Select all
if (strlen($input) > 6) and ctype_alpha($input))
Re: Trouble With Regex
Posted: Tue Nov 21, 2006 12:39 pm
by volka
But that doesn't check for
Superman859 wrote:It needs to be at least 6 characters long, have at least 1 uppercase, 1 lowercase, and 1 number.
Re: Trouble With Regex
Posted: Tue Nov 21, 2006 12:41 pm
by John Cartwright
volka wrote:But that doesn't check for
Superman859 wrote:It needs to be at least 6 characters long, have at least 1 uppercase, 1 lowercase, and 1 number.
My bad, I kinda just looked at the second post.
Posted: Tue Nov 21, 2006 12:46 pm
by Superman859
Yea, it's not too bad creating one that is alphanumeric and at least 6 characters...
I'm actually writing the pattern for a Java program i was working on, which uses the same regex patterns for the most part.
I know how to do it all separately. I can check to see if it is alphanumeric and at least 6 characters in one step, and then qualify the others (at least 1 of each) using a for loop and boolean variable. However, since I have begun to learn regex, I was wondering if it's possible to do it all with one pattern. I would have thought you could, but I haven't figured it out.
Posted: Tue Nov 21, 2006 3:46 pm
by sweatje
This is probably not the best tool for the job, but just for fun, here is a regex which seems to fit the bill, tested using a
SimpleTest test case:
Code: Select all
function testSixOrMore1Up1Down1NumWithRegex() {
$regex = '~^([a-z](?=[a-z]*([A-Z]+[a-z]*[0-9]|[0-9]+[a-z]*[A-Z]))
|[A-Z](?=[A-Z]*([a-z]+[A-Z]*[0-9]|[0-9]+[A-Z]*[a-z]))
|[0-9](?=[0-9]*([A-Z]+[0-9]*[a-z]|[a-z]+[0-9]*[A-Z])))
[A-Za-z0-9]{5,}$~x';
$this->assertTrue(preg_match($regex, 'Ab1234'),'ok');
$this->assertTrue(preg_match($regex, 'AAbb11'),'ok');
$this->assertTrue(preg_match($regex, 'bb11AA'),'ok');
$this->assertTrue(preg_match($regex, '11bbAA'),'ok');
$this->assertFalse(preg_match($regex, '123456'),'all num, should fail');
$this->assertFalse(preg_match($regex, 'AaBbCc'),'all alpha, should fail');
$this->assertFalse(preg_match($regex, 'Ab124'),'too short, should fail');
$this->assertFalse(preg_match($regex, 'ab1234'), 'no upper, should fail');
$this->assertFalse(preg_match($regex, 'Ab1!34'), 'special char, should fail');
}
Posted: Tue Nov 21, 2006 4:45 pm
by RobertGonzalez
Found
this one on
RegExpLib. Searched for 'password'
Code: Select all
<?php
// Makes sure there is at least one UPPER case letter, one lower case and one number, and that it is between 4 and 8 chars long
// Change the 4 and 8 to your min and max length requirements
$pattern = '^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$';
?>
Posted: Tue Nov 21, 2006 8:36 pm
by sweatje
Hi Everah,
That is a much better regex, it would fail for the last test case, but can be easily tweaked to work correctly:
Code: Select all
function testSixOrMore1Up1Down1NumWithRegex() {
$regex = '~^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,}$~';
$this->assertTrue(preg_match($regex, 'Ab1234'),'ok');
$this->assertTrue(preg_match($regex, 'AAbb11'),'ok');
$this->assertTrue(preg_match($regex, 'bb11AA'),'ok');
$this->assertTrue(preg_match($regex, '11bbAA'),'ok');
$this->assertFalse(preg_match($regex, '123456'),'all num, should fail');
$this->assertFalse(preg_match($regex, 'AaBbCc'),'all alpha, should fail');
$this->assertFalse(preg_match($regex, 'Ab124'),'too short, should fail');
$this->assertFalse(preg_match($regex, 'ab1234'), 'no upper, should fail');
$this->assertFalse(preg_match($regex, 'Ab1!34'), 'special char, should fail');
}
Posted: Wed Nov 22, 2006 11:13 pm
by Superman859
Yes, that is certainly a better pattern.
That makes use of lookahead's, correct? I don't know much about them as I'm just starting to learn regex, but it seems to make sense in this case and I actually understand the pattern![/img]
Posted: Thu Nov 23, 2006 4:20 am
by Jenk
Posted: Fri Dec 08, 2006 3:04 pm
by kaisellgren
This will check that $a has atleast 6 letters and max 25 while it checks that $a has atleast 1 low letter, 1 upp letter and 1 number. It also accepts characters like å, ß, etc...
Code: Select all
if (preg_match("/\p{Ll}.*\p{Lu}|\p{Lu}.*\p{Ll}/",$a) && preg_match("/\d/",$a) && preg_match("/^\p{L}{6,25}$/",$a))
echo "$a is valid!";
Posted: Fri Dec 08, 2006 3:24 pm
by kaisellgren
Everah | Split regex pattern so it doesn't break the layout of the boards.
Okay. Now I spent couple of minutes on this and tested it and works great.
This regex checks you have atleast 1 lower-,1uppercahracter and 1 number. This does not check the length though.
Code: Select all
$pattern = "/([a-z][a-zA-Z0-9]*[A-Z][a-zA-Z0-9]*[\d]|[A-Z][a-zA-Z0-9]*[a-z][a-zA-Z0-9]*[\d]";
$pattern .= "|[a-z][a-zA-Z0-9]*[\d][a-zA-Z0-9]*[A-Z]|[\d][a-zA-Z0-9]*[A-Z][a-zA-Z0-9]*[a-z]|";
$pattern .= "[A-Z][a-zA-Z0-9]*[\d][a-zA-Z0-9]*[a-z]|[\d][a-zA-Z0-9]*[a-z][a-zA-Z0-9]*[A-Z])/";
preg_match($pattern, $a);
Posted: Fri Dec 08, 2006 3:46 pm
by RobertGonzalez
There is not easier or cleaner regex than that monstrosity?

Posted: Fri Dec 08, 2006 3:54 pm
by kaisellgren
Everah wrote:There is not easier or cleaner regex than that monstrosity? :wink:
Yes there is. Something I made with my two friends once we were bored. This pattern can be used for complete Email check (RFC822). Not 100% sure it works but looks like.
Code: Select all
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\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]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*))*)?;\s*)
I just love regular expressions!
Code: Select all
((I|you|we|they)\s+love|s?he\s+loves)\s*reg(ular expressions?|ex(p|es)?)