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

Mary-Kate Olsen
Release: 2024-11-11 01:29:02
Original
541 people have browsed it

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
Copy after login

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!

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