I want to validate an email in a function with an email that meets the following conditions:
A valid email address is of the form username@domain.tld, such that:
- The username consists of only English characters, numbers,
underscores and periods. - The domain consists of only English characters or numbers.
- tld is a three-letter word of English characters.
- English characters can be lowercase or uppercase.
I have written the following code but I don’t know why it doesn’t work?
import re
def validate_email(email):
regex = re.compile(r'^([a-zA-Z0-9_.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z])$')
if re.fullmatch(regex, email):
return True
else:
return False
I want the domain to be exactly three letters like com tld edu …
>Solution :
you have added this \.([a-zA-Z]){3}$
def validate_email(email):
regex = re.compile(r'^([a-zA-Z0-9_.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{3})$')
if re.fullmatch(regex, email):
return True
else:
return False
now is working