Learn Regular Expressions by Building a Password Generator - Step 44

Tell us what’s happening:

Hi. If raw strings disable escape characters as explained in the lesson, wouldn’t the backslash (“/”) be recognized as just a mere single character, therefore the RegEx instance would literally search for the two-characters, “/.”? Why does the backslash seem to be ignored when being searched in the “quote” ? (This question is about the bottom part of the code).

Appriciate all your support from all over the world so that I can progress
Sincerely,
Nozomu

Your code so far

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)
        
        constraints = [
            (nums, '[0-9]'),
            (lowercase, '[a-z]'),
            (uppercase, '[A-Z]'),
            (special_chars, '')
        ]        

    return password
    
# new_password = generate_password(8)
# print(new_password)

# User Editable Region

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

# User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn Regular Expressions by Building a Password Generator - Step 44

There are kind of two escapes. One is escaping special characters in string (ie. new line character), second is escaping characters that have special meaning in regex.

Dot have special meaning in regex, so to match only the literal dot, it needs to be escaped.

>>> re.findall(r'\.', 'Not all those who wander are lost.')
['.']

>>> re.findall(r'.', 'Not all those who wander are lost.')
['N', 'o', 't', ' ', 'a', 'l', 'l', ' ', 't', 'h', 'o', 's', 'e', ' ', 'w', 'h', 'o', ' ', 'w', 'a', 'n', 'd', 'e', 'r', ' ', 'a', 'r', 'e', ' ', 'l', 'o', 's', 't', '.']

Dear Sanity,

Thank you for your reply.
I’m sorry if I caused any confusion. What I wanted to ask is:

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

Why does this search for only the ‘.’ character instead of literally searching for ‘\.’? Since the ‘r’ prefix turned the backslash and period into a regular character, shouldn’t it be searching for ‘\.’ in the string?

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

the . is a special character in regex, try to remove the \ and see what it matches. \. make the period a regular period, and it matches only the literal character .

Dear ILM,

Thank you so much for correcting my text.
Also, I have to apologize for not knowing that escaping was necessary to display backslashes on this forum as well. I have just updated my reply to Sanity, so could you please take another look at my post?

To answer in a simplest way - it searches for literal dot ., because dot is escaped in regex.

Dot is not the best example, as there’s nothing special about dot in string. With or without backslash in context of string alone there isn’t much difference.

>>> print('\.')
\.
>>> print(r'\.')
\.
>>> print(r'\\.')
\\.
>>> print('\\.')
\.

That changes if later that string is used in context of regex.

Here’s another example, with special character in string itself:

>>> print('\n')


>>> print('\\n')
\n
>>> print(r'\\n')
\\n
>>> print(r'\n')
\n