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,


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:

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})$"

import re

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

test_str = ("something123@somedomain123.com1\n"

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 1 found
Match 2 found

Have Questions? Post them here!
Try Out Code2care Dev Tools:


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.