Problem of the Day
A new programming or logic puzzle every Mon-Fri

Password Regex

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]).*$

^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$

If you really like regex puzzles give this one a try.

Permalink: http://problemotd.com/problem/password-regex/

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

    static string GeneratePassword(int intLength)
    {
        bool blnHasLowercase = false;
        bool blnHasUppercase = false;
        bool blnHasNumber = false;
        Dictionary<string, string> dctCharacters; 
        char[] aryChars;
        string[] aryKeys;
        string strCharacterKey;       
        byte[] aryBytes = new byte[intLength * 8];
        char[] aryPasswordChars = new char[intLength];
        new RNGCryptoServiceProvider().GetBytes(aryBytes);
        Random objRandom = new Random();
        object syncLock = new object();
        dctCharacters = new Dictionary<string, string>();
        for (int i = 0; i < intLength; i++)
        {
            dctCharacters.Clear();
            //ensure we at least get lowercase/uppercase/number in the password
            if ((intLength - i <= 3) && (!blnHasLowercase || !blnHasUppercase || !blnHasNumber))
            {
                if (blnHasLowercase == false)
                    dctCharacters.Add("Lowercase", "abcdefghijklmnopqrstuvwxyz");
                if (blnHasUppercase == false)
                    dctCharacters.Add("Uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                if (blnHasNumber == false)
                    dctCharacters.Add("Numbers", "0123456789");
            }
            else
            {
                dctCharacters.Add("Lowercase", "abcdefghijklmnopqrstuvwxyz");
                dctCharacters.Add("Uppercase", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
                dctCharacters.Add("Numbers", "0123456789");
                dctCharacters.Add("Characters", "!£$%^&*(){}[]@~#<>,.");
            }
            aryKeys = new string[dctCharacters.Keys.Count];
            dctCharacters.Keys.CopyTo(aryKeys, 0);
            lock (syncLock)
            {
                 strCharacterKey = aryKeys[objRandom.Next(0, dctCharacters.Count)];
            }
            if (strCharacterKey == "Lowercase")
                blnHasLowercase = true;
            if (strCharacterKey == "Uppercase")
                blnHasUppercase = true;
            if (strCharacterKey == "Numbers")
                blnHasNumber = true;
            aryChars = dctCharacters[strCharacterKey].Distinct().ToArray();
            aryPasswordChars[i] = aryChars[BitConverter.ToUInt64(aryBytes, i * 8) % (uint)aryChars.Length];
        }
        return new string(aryPasswordChars);
    }
    

    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.

    import random
    password = ""
    numbers = "1234567890"
    lowerCase = "abcdefghijklmnopqrstuvwxyz"
    upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    for i in range(3):
        password += random.choice(numbers)
        password += random.choice(lowerCase)
        password += random.choice(upperCase)
    password = ''.join(random.sample(password, len(password)))
    
    print password
    

    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

Content curated by @MaxBurstein