在正则表达式中,如果需要匹配包含单引号的字符串,可以使用转义字符 '' 将单引号转义,如下所示:

import re

pattern = ''([^']*)''
text = 'This is a 'sample' text.'

matches = re.findall(pattern, text)
print(matches)

输出结果为:['sample']

在上述代码中,'([^']*)' 是正则表达式模式,它会匹配包含在单引号中的任意字符(除了单引号本身)。使用 re.findall() 方法可以找到所有符合模式的匹配项。


原文地址: http://www.cveoy.top/t/topic/o218 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录