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

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-11 01:29:02548browse

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

Matching a Literal Dot in a Regular Expression

In regex, a period (.) typically represents any character. However, when working with Python's raw format strings (prefixed with 'r'), a literal dot needs to be escaped. This is because escaped characters in raw strings are treated literally, overriding their special meanings in regex.

To match a dot in Python using regex, it must be preceded by an escape character (""). For instance:

import re

text = "blah blah blah test.thisis@example.com blah blah"
match = re.search(r"\.this", text)  # Escape the literal dot with "\."

if match:
    print(match.group())  # Output: .this

The above is the detailed content of How to Match a Literal Dot in a Python Regular Expression?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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