Email address verification is a very common requirement, whether it is developing a website or writing an email client program. Python’s built-in regular expression module re is a powerful tool for dealing with such problems. This article will introduce how to use Python regular expressions for email address verification.
Before we begin, let’s first understand the basic format of the email address so that it can be more accurate when using regular expressions for verification.
A standard email address consists of two parts, namely "username" and "domain name". Among them, the "username" part usually consists of letters, numbers, underscores, periods, hyphens and other characters, while the "domain name" part consists of the host name and domain name, connected by the "@" symbol in the middle.
For example, a common email address might be "james@example.com". Among them, "james" is the user name and "example.com" is the domain name.
In addition to this basic format, there are many complex email address formats, such as internationalized domain names, IP addresses, etc. But usually, we only need to deal with general email address formats.
Python's built-in re module provides many regular expression functions, the most commonly used of which are re.match() and re.search(). Both functions can be used to find a pattern in a piece of text.
In order to verify the email address, we need to use regular expressions to match the format of the email address. The following is a basic email address regular expression:
import re pattern = re.compile(r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}')
The meaning of this regular expression is:
In the above regular expression, we use the two quantifier symbols {2,} and {2,}. They both represent one or more repetitions, but there must be at least one, and {2,} There must be at least two.
Of course, the above regular expression does not cover all email address formats, but it can match most email addresses that meet the standards.
Next, we can use the match() or search() function of the re module to verify the email address.
email = "james@example.com" if re.match(pattern, email): print("Valid email address.") else: print("Invalid email address.")
We take "james@example.com" as an example and use the match() function for verification. If the address matches our regular expression, output "Valid email address."; otherwise, output "Invalid email address."
If you want to verify a group of email addresses, you can save them in a list or file:
emails = ['james@example.com', 'jane.doe@example.com', 'john_doe@mail.com'] for email in emails: if re.match(pattern, email): print(email, "is a valid email address.") else: print(email, "is an invalid email address.")
In this way, we can quickly verify a group of email addresses.
Using Python regular expressions for email address verification is a very common task. In this article, we introduce some basic email address formats and their regular expressions, and how to use Python's built-in re module to verify email addresses. Of course, if you need more complex verification functions, you can also combine it with other Python modules. I hope this article can help you when dealing with email address verification issues!
The above is the detailed content of How to use Python regular expressions for email address verification. For more information, please follow other related articles on the PHP Chinese website!