Back to Freecodecamp

Step 47

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

latest1.7 KB
Original Source

--description--

In a character class, you can combine multiple ranges by writing one range after another inside the square brackets (without any additional characters). For example: [a-d3-6] is the combination of [a-d] and [3-6].

Now, modify the last regex pattern to match any non-alphanumeric character. Combine the a-z, A-Z, and 0-9 ranges into a single character class and add a ^ as the first character to negate the pattern.

--hints--

You should complete your fourth pattern to match any non-alphanumeric character.

js
({ test: () => assert.match(code, /constraints\s*=\s*\[\s*\(\s*nums\s*,\s*r("|')\\d\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("|')\[\^a-zA-Z0-9\]\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, r'\d'),
            (lowercase, r'[a-z]'),
            (uppercase, r'[A-Z]'),
            (special_chars, r'')
        ]        
--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))