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 username can consist of roman alpabets upper letter A-Z, lower a-z, number 0-9, underscores and dots.
  2. The Domain name: most domains consists of roman alpabets upper letter A-Z, lower a-z, number 0-9, 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 alpabets 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

Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


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 Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency