正则表达式 " />

扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

正则表达式如何匹配特殊字符

扬州沐宇科技
2024-06-14 17:17:34
正则表达式

要匹配特殊字符,可以使用反斜杠(\)来转义特殊字符。例如,如果想匹配句号(.)或星号(*),可以使用.*来表示这两个特殊字符。下面是一个简单的例子:

假设我们想匹配一个包含句号的字符串:

import re

pattern = r'\.'
text = "This is a sentence with a period."

result = re.search(pattern, text)

if result:
    print("Match found!")
else:
    print("No match found.")

在上面的例子中,我们使用正则表达式模式r’.'来匹配句号。如果字符串中包含句号,则会输出"Match found!"。

扫码添加客服微信