Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Validate email with regex having exactlly 3 letters in TLD

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading