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