Home > Backend Development > Python Tutorial > How to Match Literal Dot Characters in Email Addresses Using Regular Expressions?

How to Match Literal Dot Characters in Email Addresses Using Regular Expressions?

Barbara Streisand
Release: 2024-11-08 05:38:01
Original
499 people have browsed it

How to Match Literal Dot Characters in Email Addresses Using Regular Expressions?

Using Regular Expressions to Identify Dot (.) Characters in Email Addresses

In data parsing scenarios, it is often necessary to extract specific elements from strings, such as email addresses. Regular expressions offer a powerful tool for such tasks.

Matching Literal Dot Characters

The dot (.) is a metacharacter in regular expressions, meaning it represents any character. However, to match a literal dot in a Python raw string (denoted by r"" or r''), it must be escaped as r".".

For instance, consider the following string:

"blah blah blah [email protected] blah blah"
Copy after login

To extract the email address, which includes a literal dot, we can use the following regular expression:

r"\b\w+\.\w+@\w+\.\w+"
Copy after login

Breakdown of the Regex:

  • b: Matches a word boundary (i.e., the start or end of a word).
  • w : Matches one or more word characters (e.g., letters or digits).
  • .: Matches a literal dot (period).
  • w : Matches one or more word characters again.
  • @: Matches the at symbol (@) in email addresses.
  • w : Matches one or more word characters for the domain name.
  • .: Matches a literal dot (period) separating the domain name and suffix.
  • w : Matches one or more word characters for the domain suffix.

Using this regex, we can extract the email address from the given string:

import re

text = "blah blah blah [email protected] blah blah"
email = re.findall(r"\b\w+\.\w+@\w+\.\w+", text)
print(email)  # Output: ['[email protected]']
Copy after login

The above is the detailed content of How to Match Literal Dot Characters in Email Addresses Using Regular Expressions?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template