This link has been floating around on the internet so I figured we'd make it a problem and try to solve it in 2 parts. Part one is figuring out what the regex does and part two is writing a program to generate a valid password to fit the regex.
^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$
If you really like regex puzzles give this one a try.
Comments:
Daniel - 10 years, 7 months ago
My last comment seems to have disappeared! :(
Pattern means 8 characters of more with at least one number, one uppercase, and one lowercase letter.
My C# Solution
reply permalink
Nick Krichevsky - 10 years, 7 months ago
The purpose is to have a password with at least 8 chars and it must contain at the very least, a number, a capital letter, and a lowercase letter. My python solution.
reply permalink
Daniel - 10 years, 7 months ago
Python has some great random functions.
reply permalink
David - 10 years, 7 months ago
I like how clean this looks
I added the capability to generate a random length for the password as well as including any valid ASCII character.
You can condense my code, at the cost of a little readability. My method to ensure meeting the required character components was to generate a string 3 characters shorter than I desired and insert the three required types of characters at randomly selected locations.
''' import random
randAscii = random.randint(32,126) requiredNumber = random.randint(48,57) requiredLower = random.randint(97,122) requiredCapital = random.randint(65,90) passwordLength = random.randint(8,32)
newPassword = str(unichr(randAscii)) while len(newPassword) < passwordLength - 3: randAscii = random.randint(32,126) newPassword += str(unichr(randAscii))
insertPoint = random.randint(0,len(newPassword)) newPassword = newPassword[:insertPoint] + str(unichr(requiredNumber)) + newPassword[insertPoint:] insertPoint = random.randint(0,len(newPassword)) newPassword = newPassword[:insertPoint] + str(unichr(requiredLower)) + newPassword[insertPoint:] insertPoint = random.randint(0,len(newPassword)) newPassword = newPassword[:insertPoint] + str(unichr(requiredCapital)) + newPassword[insertPoint:]
print newPassword '''
reply permalink