How to Validate Email Address in Python with Regular Expression
Email address validation is a common task in programming, and Regular Expression is a powerful tool to achieve it. Python is a popular general-purpose programming language, and it comes with built-in support for Regular Expression. In this article, we will discuss how to validate email addresses in Python using Regular Expression.
What is Regular Expression?
Regular Expression or Regex is a pattern matching language that allows us to search for specific patterns in a string. It is a powerful tool used for data validation, text processing, and search-and-replace operations. Regular Expression is supported by most modern programming languages, including Python. A Regular Expression pattern typically consists of a combination of characters, metacharacters, and quantifiers.
Email Address Validation Using Regular Expression
Validating an email address using Regular Expression in Python involves creating a pattern that matches the email address format. An email address typically consists of two parts: the local part and the domain part. The local part is the username, and the domain part is the domain name. Both parts can have specific restrictions on the characters used. The following is an example of a Regex pattern that matches a valid email address format:
import re
pattern = r'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$'
def is_valid_email(email):
if re.match(pattern, email):
return True
else:
return False
Let’s break down the above pattern to understand each part:
^
and$
are anchors that match the beginning and end of the string, respectively.[A-Za-z0-9._%+-]+
matches the local part of the email address. It allows letters, digits, dots, underscores, percent signs, plus signs, and hyphens.@
matches the at sign character.[A-Za-z0-9.-]+
matches the domain part of the email address. It allows letters, digits, dots, and hyphens.\.
matches the dot character (escaped with a backslash because it has a special meaning in Regex).[A-Z|a-z]{2,}
matches the top-level domain part of the email address. It requires at least two letters and allows uppercase and lowercase letters.
Now that we have the Regex pattern, we can use it to validate an email address by calling the is_valid_email()
function with the email address as an argument. The function returns True if the email address is valid or False if it is invalid.
# Test if an email address is valid
print(is_valid_email("example@email.com")) # True
print(is_valid_email("example.email.com")) # False
print(is_valid_email("example@.com")) # False
Conclusion
In this article, we have discussed how to validate email addresses in Python using Regular Expression. Regular Expression is a powerful tool for text processing and data validation, and it is supported by most modern programming languages, including Python. We have shown an example of a Regex pattern that matches a valid email address format and demonstrated how to use it to validate an email address. By following the guidelines presented in this article, you can write your own email address validation code in Python using Regular Expression.