ios - 正则表达式捕获 URL 中最后一个“/”之后的所有内容
问题描述
let url1 = "https://cdn0.tnwcdn.com/files/2020/01/exmaple1.jpg"
let url2 = "http://www.clker.com/cliparts/1/7/b/9/11974343961963318467fl.png"
let url3 = "https://images.squarespace-cdn.com/content/01+Seth+Easy+v1.gif"
我怎样才能在最后一个斜线之后捕捉到所有东西,所以在这种情况下:
exmaple1.jpg
11974343961963318467fl.png
01+Seth+Easy+v1.gif
解决方案
使用components(separatedBy:)
并从中获取last
组件,即
url1.components(separatedBy: "/").last //"exmaple1.jpg"
url2.components(separatedBy: "/").last //"11974343961963318467fl.png"
url3.components(separatedBy: "/").last //"01+Seth+Easy+v1.gif"
或者您可以在使用创建lastPathComponent
的URL
实例上使用url1, url2 and url3
URL(string: url1)?.lastPathComponent //"exmaple1.jpg"
URL(string: url2)?.lastPathComponent //"11974343961963318467fl.png"
URL(string: url3)?.lastPathComponent //"01+Seth+Easy+v1.gif"
推荐阅读
- node.js - 如何在 nodejs Web 应用程序的后端执行 shell 脚本?
- sublimetext3 - 在 Mac 上的 sublime 中为两列添加逗号和引号
- python - 通过使用pyspark将列转换为行来解析数据框中的Json字符串列表?
- javascript - vue-query 返回 ObjectRefImpl 而不是数据
- android - Android:在列表中搜索与使用 DAO 搜索
- powerbi - PDF 文本文件(数据加载、提取、替换、修订文本文件与旧版本的比较、合并文本更改的摘要)
- maxscript - TabIndex - dotNet 控件和本机控件在 Maxscript 中不起作用
- reactjs - 输入文本时出现意外的蓝色边框反应选择
- android - SHA-1 在生成消息时无法在 Firebase 中运行,您能告诉我如何获取新的 SHA-1 吗?
- javascript - 当它是一个函数时,为什么我会得到“this.XXXX 不是一个函数”?