python 正则提取网址中图片名字,目前只支持jgp,git,png三种图片格式

import re
def get_image_name(url):
    pattern = re.search("([|.|\w|\s|-])*?.(jpg|gif|png)", url)
    if not pattern:
        return
    image_name = pattern.group()
    return image_name

正则解释

\w 匹配字母数字及下划线

\s 匹配任意空白字符,等价于 [ \t\n\r\f]。、

- 匹配网址中-

. 匹配网址中.

[] 用来表示一组字符,单独列出:[amk] 匹配 'a','m'或'k'

测试样例

print(get_image_name("https://sc04.alicdn.com/kf/HTB1DO6zALiSBuNkSnhJq6zDcpXa7.jpg"))
print(get_image_name("https://sc04.alicdn.com/kf/HTB1eISob56guuRkSmLyq6AulFXat.jpg"))
print(get_image_name("https://sc04.alicdn.com/kf/H752f638bcb5b40499deaf35b1c28edbaz.jpg"))
print(get_image_name("https://sc04.alicdn.com/kf/Hf844961dc5344c83ae7ed6a7d974fe33m.jpg"))
print(get_image_name("https://sc01.alicdn.com/kf/HTB1pKcVS/230438700/HTB1pKcVSpXXXXa8XXXXq6xXFXXXs.jpg"))
print(get_image_name("https://sc04.alicdn.com/kf/HTB10.SSn.R1BeNjy0Fmq6z0wVXa5.jpg"))
print(get_image_name("https://farm4.staticflickr.com/3894/15008518202.c265dfa55f.h.png"))
print(get_image_name("http://farm4.staticflickr.com/3894/15008518202_c265dfa55f_h.png"))
print(get_image_name("
 

HTB1DO6zALiSBuNkSnhJq6zDcpXa7.jpg
HTB1eISob56guuRkSmLyq6AulFXat.jpg
H752f638bcb5b40499deaf35b1c28edbaz.jpg
Hf844961dc5344c83ae7ed6a7d974fe33m.jpg
HTB1pKcVSpXXXXa8XXXXq6xXFXXXs.jpg
HTB10.SSn.R1BeNjy0Fmq6z0wVXa5.jpg
15008518202.c265dfa55f.h.png
15008518202_c265dfa55f_h.png
15008518202-c265dfa55f-h.jpg

参考网址:

https://regexr.com/3g1v7

本文地址: http://chenxm.cc/article/1339.html
版权声明: 本文为原创文章,版权归  陈新明  所有,欢迎分享本文,转载请保留出处!
上一篇: python 获取多线程的返回值
下一篇: 已经是最新一篇了
发表评论

还没有留言,还不快点抢沙发?