Im writing a python program using regex to find email addresses. re.findall function is giving wrong output whenever I try to use round brackets for grouping. Can anyone point out the mistake / suggest an alternate solution?
Here are two snippets of code to explain -
pat = "[\w]+[ ]*@[ ]*[\w]+.[\w]+"
re.findall(pat, '[email protected] .rtrt.. [email protected] ')
gives the output
['[email protected]', '[email protected]']
However, if I use grouping in this regex and modify the code as
pat = "[\w]+[ ]*@[ ]*[\w]+(.[\w]+)*"
re.findall(pat, '[email protected] .rtrt.. [email protected] ')
the output is
['.com', '.com']
To confirm the correctness of the regex, I tried this specific regex (in second example) in http://regexpal.com/ with the same input string, and both the email addresses are matched successfully.
[email protected]. I expect that allowing spaces around the@(which is of course invalid) is done on purpose?