我想创建一个正则表达式来提取网址的文件名
https://example.net/img/src/img.jpg
我想提取img1.jpg
我使用Python中的urlparse,但它以这种方式提取路径
img/src/img.jpg
如何使用正则表达式提取文件名
最佳答案
使用str.split
和负索引
url = "https://example.net/img/src/img.jpg"
print(url.split("/")[-1])
输出:
img.jpg
或使用os.path.basename
import urlparse, os
url = "https://example.net/img/src/img.jpg"
a = urlparse.urlparse(url)
print(os.path.basename(a.path)) #--->img.jpg
关于python - 使用正则表达式提取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245419/