-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
  • I am using both to extract domain name from email address like abc@google.com,def@rediff.com ..etc, Does both pattern gives the same result ? @Whothehellisthat – Salman Aug 28 '16 at 15:31
  • Well, you said that the first one "is not giving me the intended result." Why are you asking me, if you've already decided the first one doesn't work as desired? How do you know that? Give us some examples. – Whothehellisthat Aug 28 '16 at 15:34
  • I understood,in the first pattern i am using lazy search, – Salman Aug 28 '16 at 15:59