Challenge/Lesson #28 of Python for Everybody course

Just wanted to point out a small error in the code regarding the questions asked below the video lesson. I think the regex in the lst findall function/method should be \S+@\S+ and not \\S+@\\S+. Correct me if I’m wrong :smiley:

Challenge Link:

Hello there,

There is no error here. This just has to do with the way regex needs to be represented in Python strings: re — Regular expression operations — Python 3.12.0 documentation

The double backslash is not necessary in raw strings.

More information at the top of that page:

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal. Also, please note that any invalid escape sequences in Python’s usage of the backslash in string literals now generate a DeprecationWarning and in the future this will become a SyntaxError. This behaviour will happen even if it is a valid escape sequence for a regular expression.

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

Hope this clarifies

I see now. Thank you for clarifying!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.