How to use Python regular expressions for email address verification

WBOY
Release: 2023-06-22 08:37:54
Original
1892 people have browsed it

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.

Format of email address

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.

Use regular expressions for verification

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,}')
Copy after login

The meaning of this regular expression is:

  • : matches word boundaries to ensure that we only match the complete email address .
  • [A-Za-z0-9._% -]: Match one or more letters, numbers, periods, underscores, hyphens, plus signs, and percent signs.
  • @: Matches the "@" character.
  • [A-Za-z0-9.-]: Match one or more letters, numbers, periods, and hyphens.
  • .: Matches a "." character.
  • [A-Z|a-z]{2,}: Match at least two letters.

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.")
Copy after login

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.")
Copy after login

In this way, we can quickly verify a group of email addresses.

Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template