
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
Have Questions? Post them here!
- tkinter - Hello World! Program
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew
- How to install SpaCy (NLP Library) on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- How to uninstall pip Python packages
- 3 Ways to find if element is present in a List in Python
- How to Convert Python String to DateTime Object
- Python f-strings Formatted String Literals Syntax and Examples
- Where does brew install python in macOS
- Take input argument from command line in Python Programming
- Advanced print() Function Tutorial and Techniques for Python Developers
- How to add borders to tkinter label text
- Whats new in Python 3.10 Pre-release
- Float built-in function in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- How to check if Key Exists in Python Dictionary?
- Read a file line by line in Python Program
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- What is the Max and Minimum Value of int type in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- Fix: TypeError: can only concatenate str (not int) to str in Python
- How to take user input from the console in a Python program
- [Fix] TypeError: str object is not callable in Python
- 3 Python program to add two numbers
- How to delete a file using Python code example
- [Microsoft Teams] You're not on Teams yet, but you can set it up for your organization. - Teams
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years - Java
- How to mute or disable Twitter Fleet in feeds - Twitter
- How to hide or remove quick launch left navigation from SharePoint Online Modern site page - SharePoint
- SharePoint Server 2016 IT Preview Deprecated Removed features - SharePoint
- What is BTS? What is BTS A.R.M.Y? What is its meaning? - BTS
- Eclipse: Updating Maven Project. Unsupported IClasspathEntry kind=4 - Eclipse
- Install the minimal Linux on Docker (only 5 mb Alpine Linux) - Docker