2

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.

1 Answer 1

4

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".

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the detailed answer. So it doesn't make any difference then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.