Back to Freecodecamp

Step 45

curriculum/challenges/english/blocks/learn-regular-expressions-by-building-a-password-generator/6564daa2725b9b8420141b1c.md

latest1.4 KB
Original Source

--description--

Now, turn the four patterns from the constraints list into raw strings.

--hints--

You should turn the four strings inside the constraints list into raw strings.

js
({ test: () => assert.match(code, /constraints\s*=\s*\[\s*\(\s*nums\s*,\s*r("|')\[0-9\]\1\s*\)\s*,\s*\(\s*lowercase\s*,\s*r("|')\[a-z\]\2\s*\)\s*,\s*\(\s*uppercase\s*,\s*r("|')\[A-Z\]\3\s*\)\s*,\s*\(\s*special_chars\s*,\s*r("|')\4\s*\)\s*,?\s*\]/) })

--seed--

--seed-contents--

py
import re
import secrets
import string


def generate_password(length, nums, special_chars, uppercase, lowercase):
    # Define the possible characters for the password
    letters = string.ascii_letters
    digits = string.digits
    symbols = string.punctuation

    # Combine all characters
    all_characters = letters + digits + symbols

    while True:
        password = ''
        # Generate password
        for _ in range(length):
            password += secrets.choice(all_characters)
--fcc-editable-region--        
        constraints = [
            (nums, '[0-9]'),
            (lowercase, '[a-z]'),
            (uppercase, '[A-Z]'),
            (special_chars, '')
        ]        
--fcc-editable-region--
    return password
    
# new_password = generate_password(8)
# print(new_password)

pattern = r'\.'
quote = 'Not all those who wander are lost.'
# print(re.findall(pattern, quote))