-3

I am using the below regex for getting domain from email address: re.findall('@(.+?)',str), which is not giving me the intended result.

I have got the correct regex: re.findall('@(\w.+)',str).

Please explain the difference between the patterns.

vijayst
  • 20,359
  • 18
  • 69
  • 113
Salman
  • 9
  • 5
  • 3
    Always remember to Google first. A query for `Regex to get domain from email` has 4.4 million results. Surely one of them helps solve this super common problem. – Pekka Aug 28 '16 at 13:39

1 Answers1

0

The main difference is the way it's matching the actual domain.

.+?

  • . Matches any non-newline character.
  • + Matches the previous element (.) one or more times.
  • ? In this case, as it's after a repeater, it makes it "lazy." Without this, it would be "greedy", matching as many times as possible. When a repeater is "lazy" it matches as few times as possible.

\w.+

  • \w Matches any "word character" (generally alphabetical upper- and lower-case, and underscores).
  • . Matches any non-newline character.
  • + Repeats the previous element (.) one or more times. And because there is no ?, it will match as many times as possible.

That should outline the differences between the two. If you have examples that you wanted to match or not match, and add them to the original post, I can help with a further explanation on why one works while the other doesn't for those cases.

Whothehellisthat
  • 2,072
  • 1
  • 14
  • 14