I’m working my way through the Scientific Computing with Python track taught by Doc Chuck Severance. I just finished watching the “Regular Expressions: Matching and Extracting Data” video, and I’m confused about the question that comes with the video. In the tutorial Doc Severance uses the example:
y = re.findall (‘\S+@\S+’, x)
print(y)
I understood this to mean find "any non-whitespace character ‘\S’ one or more times ‘+’, followed by ‘@’, followed by any non-whitespace character ‘\S’ one or more times ‘+’.
But then in the accompanying question, this example is used:
import re
s = ‘A message from csev@umich.edu to cwen@iupui.edu about meeting @2PM’
lst = re.findall(‘\\S+@\\S+’, s)
print(lst)
What I don’t understand is why there are two backslashes before the two ‘S’ characters. The correct answer for the output when this code is run is:
[‘csev@umich.edu’, ‘cwen@iupui.edu’]
When I run the code myself, whether I leave one backslash or two before the ‘S’ characters, the output is the same. I thought backslash was the Regex escape character, so I expected if there was a backslash before a backslash that would mean to search for a backslash, which would mean that ‘\\S+@\\S+’ would translate as find “a backslash ‘\’, followed by ‘S’ one or more times ‘+’, followed by ‘@’ , followed by a backslash ‘\’, followed by ‘S’ one or more times ‘+’”. But apparently I’m misunderstanding something here. Can someone please explain why code with “\\S” and code with “\S” produce the same output?
EDIT: After I posted this, I noticed that in this post where I had put double-backslashes only a single backslash was displaying after I posted it. To get it to display double-backslashes I had to edit the post and type triple-backslashes where I wanted double-backslashes displayed. What is up with that? And does it have something to do with my initial question?