Home > Backend Development > Python Tutorial > How to Match a Literal Dot in a Regular Expression with Python?

How to Match a Literal Dot in a Regular Expression with Python?

DDD
Release: 2024-11-11 14:57:03
Original
329 people have browsed it

How to Match a Literal Dot in a Regular Expression with Python?

Matching a Literal Dot with Regular Expressions

In Python, matching a literal dot (.) using raw strings (e.g., r"string") requires escaping to avoid treating it as a metacharacter that matches any character.

To match the string "test.this" from the input "blah blah blah [email protected] blah blah", you can use the following regular expression:

import re

pattern = r"\b\w\.\w@.*"
match = re.findall(pattern, "blah blah blah [email protected] blah blah")
print(match)
Copy after login

Here's a breakdown of the regular expression:

  • b: Matches a word boundary (start or end of a word).
  • w: Matches any word character (letter, digit, or underscore).
  • .: Matches a literal dot (escaped to prevent it from matching any character).
  • w: Matches any word character again.
  • @: Matches the @ symbol.
  • .*: Matches any number of characters (including none), which captures the remaining part of the email address.

The re.findall() function returns a list of all matching substrings in the input string. In this case, it will return a list containing the matched string "test.this@...".

The above is the detailed content of How to Match a Literal Dot in a Regular Expression with Python?. 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