i'm asked to write regular expression which can catch multi-domain email addresses and implement it in python. so i came up with the following regular expression (and code;the emphasis is on the regex though), which i think is correct:
import re
regex = r'\b[\w|\.|-]+@([\w]+\.)+\w{2,4}\b'
input_string = "hey my mail is [email protected]"
match=re.findall(regex,input_string)
print match
now when i run this (using a very simple mail) it doesn't catch it!! instead it shows an empty list as the output. can somebody tell me where did i go wrong in the regular expression literal?
['def.'](which is the only bit you're capturing with()). b) the regex is not right, you can't use|like that inside a character class - inside[]it matches the pipe character literally, it doesn't do either-or, and\bdoesn't match at the end of a string, and the regex is broken for addresses like[email protected]which don't have a 2-4 digit TLD.findallreturns the capture groups, not the full match. Change([\w]+\.)to(?:\w+\.)to change the parens to non-capturing (also removing the superfluous but harmless brackets;\wis a character class by itself).