
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 username can consist of roman alpabets upper letter A-Z, lower a-z, number 0-9, underscores and dots.
- The Domain name: most domains consists of roman alpabets upper letter A-Z, lower a-z, number 0-9, 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 alpabets 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
- How to add two float numbers in Python
- tkinter - Hello World! Program
- Convert Float to String in Python
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- How to List all Packages installed using pip [Python]
- Check if String Contains a Substring - Python
- Change label (text) color in tkinter
- Where does brew install python in macOS
- Validate email address in Python using regular expression (regex)
- How to uninstall pip Python packages
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Fix command not found pip or pip3 on zsh shell
- Check version of pip package installer for Python
- 3 Python program to add two numbers
- Calculate discount amount python code
- How to add borders to tkinter label text
- How to install Python 3.9 using brew on Mac
- Python Program To Calculate Simple Interest (SimpleInterest.py)
- Comments in Python Programming
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to take user input from the console in a Python program
- Install and Run Jupyter Notebook on Mac (macOS)
- Tkinter - add x and y padding to label text
- 7 Python Arithmetic Operators with Examples [Tutorial]
- Python Sleep Function/Method Code Example
- [Java Threads] Should we extend Thread Class or implement Runnable interface - Java
- pip/pip3 ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied - PIP
- Microsoft Teams - Where would you like to start - Business or Personal - Teams
- How to Kill a port using bash terminal command? - Bash
- Right Align Text in Bootstrap framework - Bootstrap
- Disable Control Scroll Zoom-in and Zoom-out in Notepad++ - NotepadPlusPlus
- What is Terminal Velocity and its Formula? How to calculate it programmatically? - Python
- How to create classic site in SharePoint Online - SharePoint