【问题标题】:Regex to catch everything after last '/' in URL [duplicate]正则表达式捕获 URL 中最后一个“/”之后的所有内容 [重复]
【发布时间】:2020-02-28 10:18:45
【问题描述】:
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

【问题讨论】:

  • 网址可以使用yourUrl.lastPathComponent

标签: ios swift regex url


【解决方案1】:

使用 components(separatedBy:) 并从中获取 last 组件,即

url1.components(separatedBy: "/").last //"exmaple1.jpg"
url2.components(separatedBy: "/").last //"11974343961963318467fl.png"
url3.components(separatedBy: "/").last //"01+Seth+Easy+v1.gif"

或者您可以在使用url1, url2 and url3 创建的URL 实例上使用lastPathComponent

URL(string: url1)?.lastPathComponent //"exmaple1.jpg"
URL(string: url2)?.lastPathComponent //"11974343961963318467fl.png"
URL(string: url3)?.lastPathComponent //"01+Seth+Easy+v1.gif"

【讨论】:

    猜你喜欢
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多