首页 > 其他 > 详细

爬虫常用正则表达式

时间:2020-04-28 20:52:31      阅读:61      评论:0      收藏:0      [点我收藏+]

1.指定开头,指定结尾

str1 = "background-image: url(https://image2.pearvideo.com/cont/20200428/cont-1671582-12370181.png);"

# \b开头字符.*?结尾字符\b

res = re.search(r"\bhttp.*?png\b", str1)
print(res.group())

2.匹配所有整型数字

inventory_count = "库存5000"
res = re.search("\d+", inventory_count)

3.匹配所有浮点数

 price = 565656.23223  
price = re.search([-+]?[0-9]*\.?[0-9]+, price)

 

4.匹配无视空格和换行

str1 = "instrt into inswate"
text_data = "hjkshcsdk dsehfhk instrt into
inswate " str1_list
= str1.split(" ") str2 = "" for i in str1_list: str2 = str2 + r"%s(\s*)(?i)" % i str3 = "(?s)(?i)" + str2 response = re.search(str3.rstrip("(\s*)(?i)"), text_data)

 5.匹配或者

(png|jpg|jpeg) 就是或的关系
images = re.search(r"\bhttp.*?(png|jpg|jpeg)\b", image)

 

爬虫常用正则表达式

原文:https://www.cnblogs.com/wukai66/p/12796383.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!