【发布时间】:2016-11-25 07:55:38
【问题描述】:
我只想从此字符串中获取链接:
"<p><a href=\"https://www.youtube.com/watch?v=i2yscjyIBsk\">https://www.youtube.com/watch?v=i2yscjyIBsk</a></p>\n"
我想输出为https://www.youtube.com/watch?v=i2yscjyIBsk
那么,我该如何实现呢?
我试过了:
func matches(for regex: String, in text: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: regex)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error {
}
并尝试了这个正则表达式:"<a[^>]+href=\"(.*?)\"[^>]*>.*?</a>"
但我还是想不通。
【问题讨论】: