Reputation: 1651
I just started learning about Regex in Python. I was going through this code for extracting the email address from a string.
str = 'purple [email protected], blah monkey [email protected] blah dishwasher'
emails1 = re.findall(r'[\w\.-]+@[\w\.-]+', str)
emails2 = re.findall(r'[\w.-]+@[\w.-]+', str)
Is there any difference between the code for emails1 and emails2? I tested it on one string and both are giving the same output.
This is my first post here. Please don't mind if my post was not according to any standards.Thanks.
Upvotes: 2
Views: 69
Reputation: 1230
The only difference I see is .
and \.
, which normally does make a difference.
.
stands for "any character except newline" and \.
being the literal dot. However, in character classes like [abcd.]
which means "any of the following" some characters like .
are taken literary. Since the .
are both in charater class, there is no difference between the two.
You should escape the -
in character classes though, since it stands for a character range [a-z]
. It works in your case because it's the last character in the class, but you don't want to forget it at some point and later on wonder "what's going wrong".
Upvotes: 4