Validate email address in Python using regular expression (regex)


Python Email Address validation Code Example
Python Email Address validation Code Example

Validating an email address is the most common scenario that a developer may come across while learning a new programming language, but it's important to know that Regular Expressions are the most powerful way to validate an email address.

In order to validate email in Python, you would need to make use of the re package,

Background:

You may skip this part if you already know about it,

let's start with how the email address is structured, all the email addresses that you may have come across have the following, the unique user name and the domain, the domain consists of .com .org .uk .info .gov .edu .tv .io, etc,

  1. The Username: most of the usernames can consist of roman alphabets upper letter A-Z, lower a-z, number 0-9, underscores and dots.
  2. The Domain name: most domains consist of roman alphabets upper letter A-Z, lower a-z, number 0-9, and underscores.
  3. The Domain: as you know .com is not the only one, it could range from 2 to 4 characters is what I know, if you have come across more then the logic should be alphabet upper letter A-Z, lower a-z
  4. The @ sperator

  5. The dot seperator

Example email: username@domain.com

The Regex

Keeping all the details in mind we can come up with something like this,

  • The Username: [\w\.\_]+ (can contain any number letters, numbers, dots and underscore
  • The @ seperator: @{1} (@ should occur only once!)
  • The doman name: \w+\ (can contain any number letters, numbers)
  • The dot sperator: .{1} (should occur only once)
  • The domain: [a-zA-Z]{2,4} (should be minimum two, maximum four character long)
The Email Address validation Regex:

regex = r"^[\w\.]+@{1}\w+\.{1}([a-zA-Z]{2,4})$"

Example:
import re

regex = r"^[\w\.]+@{1}\w+\.{1}([a-zA-Z]{2,4})$"

test_str = ("something123@somedomain123.com1\n"
	"something.123@somedomain123.com1\n"
	"som_e.thing.123@someone4.org\n"
  "myusername@somewebsitedomain.com")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    print ("Match {matchNum} found {match}".format(matchNum = matchNum, match = match.group()))
Result:

Match 1 found som_e.thing.123@someone4.org
Match 2 found myusername@somewebsitedomain.com

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap