使用正则表达式查找多个网址

问题描述 投票:0回答:1

我需要在一个字符串中找到多个URL。我正在使用的正则表达式是

(https?:\/\/.*\.(?:png|jpg))

并且字符串是Newhttps://www.findregexhere.com/newimage.pngfrhttps://www.findregexhere.com/newimage.pngnewimage

这是我正在使用的代码

let regex = try NSRegularExpression(pattern: "https?:\/\/.*\.(?:png|jpg)$", options: .caseInsensitive)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))

但是结果给了我这个

https://www.findregexhere.com/newimage.pngfrhttps://www.findregexhere.com/newimage.png

swift regex
1个回答
0
投票

尝试非贪婪...

(https?:\/\/.*?\.(?:png|jpg))

演示:https://regex101.com/r/AK5foS/1

© www.soinside.com 2019 - 2024. All rights reserved.