
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,
- The Username: most of the usernames can consist of roman alphabets upper letter A-Z, lower a-z, number 0-9, underscores and dots.
- The Domain name: most domains consist of roman alphabets upper letter A-Z, lower a-z, number 0-9, and underscores.
- 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
The @ sperator
- The dot seperator
Example email: username@domain.com
The RegexKeeping 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)
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
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!