关于正则表达式的转义问题

今天python爬虫使用正则表达式匹配字符死活匹配不到结果

import re
list="hello\/world"
str=re.findall("hello\/wrold",list)
print(str)

输出为空:

[]

以上为例子,可以看出可能是有转义问题,"\"是转义字符的标配,应该是出了一些问题,尝试一下反转义

str=re.findall(r"hello\/wrold",list)

输出还是为空:

[]

emm,是猜错了吗,百度一下,原来字符转义的同时还有正则转义,再改一下

str=re.findall(r"hello\\/world",list)

输出结果.成功解决

['hello\\/world']

猜你喜欢

转载自blog.csdn.net/xinzhilinger/article/details/103069703