Trouble With Regex
Moderator: General Moderators
-
Superman859
- Forum Commoner
- Posts: 47
- Joined: Sun Oct 29, 2006 10:22 am
Trouble With Regex
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,}
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,}
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.
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.
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)
);
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
^[[:alnum:]]{6,25}^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
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Trouble With Regex
My bad, I kinda just looked at the second post.volka wrote:But that doesn't check forSuperman859 wrote:It needs to be at least 6 characters long, have at least 1 uppercase, 1 lowercase, and 1 number.
-
Superman859
- Forum Commoner
- Posts: 47
- Joined: Sun Oct 29, 2006 10:22 am
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.
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.
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');
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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}$';
?>Hi Everah,
That is a much better regex, it would fail for the last test case, but can be easily tweaked to work correctly:
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');
}-
Superman859
- Forum Commoner
- Posts: 47
- Joined: Sun Oct 29, 2006 10:22 am
Yes, lookaheads.
Here is a nice cheat sheet (PDF): http://www.phpguru.org/downloads/PCRE%2 ... 0Sheet.pdf
Here is a nice cheat sheet (PDF): http://www.phpguru.org/downloads/PCRE%2 ... 0Sheet.pdf
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
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!";- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
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.
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);- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- kaisellgren
- DevNet Resident
- Posts: 1675
- Joined: Sat Jan 07, 2006 5:52 am
- Location: Lahti, Finland.
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.Everah wrote:There is not easier or cleaner regex than that monstrosity? :wink:
Code: Select all
(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()<>@,;:".\[\] \x00-\x1F]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["()<>@,;:".\[\]]))|"(?:[^"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*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)?[ \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*)Code: Select all
((I|you|we|they)\s+love|s?he\s+loves)\s*reg(ular expressions?|ex(p|es)?)