python
Python code to validate email address using regex
To validate email addresses using python, regex can be quite helpful. You can import the python regex module - re and use it to validate the email address.
import re
def validate_email(email):
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
if ( re.fullmatch(email_pattern, email) ):
print("Email is Valid")
else:
print("Email is Invalid")
validate_email("[email protected]")
# -> prints - Email is Valid
validate_email("testdomain.com")
# -> prints - Email is Invalid
In the code snippet, we are importing the python regex module using the below code.
import re
The regex pattern that we are using to validate the email address is as below:
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
Finally, the syntax that can be used to check regular expressions against the email address text is as below.
re.fullmatch(email_pattern, email)
Was this helpful?
Similar Posts
- Validate password with and without regex in Python
- Python - regex , replace multiple spaces with one space
- Python - regex,replace all character exept A-Z a-z and numbers
- Python - regex , remove all single characters,replace all single chars,char
- Convert multiple spaces of a string to single space in python [with and without regex]
- Calculate the factorial of a number using python code
- Python Measure the execution time of small bits of Python code with the timeit module