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!