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
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to convert int to ASCII in Python
- How to make use of SQLite Module in Python?
- Split a String into Sub-string and Parse in Python
- Python: Pandas Rename Columns with List Example
- How to run Python file from Mac Terminal
- How to Exit a Loop in Python Code
- Python: How to Plot a Histogram using Matplotlib and data as list
- MD5 Hashing in Python
- Jupyter: Safari Cant Connect to the Server localhost:8888/tree
- Fix: AttributeError: str object has no attribute decode. Did you mean: encode?[Python]
- How to Read a binary File with Python
- How to add two float numbers in Python
- Python: How to install YAML Package
- Python: How to Save Image from URL
- What is Markdown in Jupyter Notebook with Examples
- How to change the Python Default version
- 33: Python Program to send an email vid GMail
- How to comment code in Python
- How to Fix AttributeError in Python
- Fix: error: Jupyter command `jupyter-nbconvert` not found [VSCode]
- How to comment out a block of code in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- Import Other Python Files Examples
- Python: How to add Progress Bar in Console with Examples
- 3 Ways to convert bytes to String in Python
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac] - Android
- Screen Mirroring iPhone to TV - iOS
- MySQL: How to Select Database via Terminal/Command Line - MySQL
- How to see storage usage details on OneDrive on Mac? - Windows
- How to install pip on macOS using terminal command [Python] - Python
- 3 Ways to Change Default 8080 Port in Spring Boot - Java
- How to Recover Deleted Messages on iPhone - iOS
- How to check your IP using bash for Windows? - Bash