我想通過一個具有多個錨標記的html字符串運行正則表達式,並構建鏈接文本字典與其href url。正則表達式來匹配錨標記和它的href
<p>This is a simple text with some embedded <a href="http://example.com/link/to/some/page?param1=77¶m2=22">links</a>. This is a <a href="https://exmp.le/sample-page/?uu=1">different link</a>.
如何提取一氣呵成<a>
標籤的文字和HREF?
編輯:
func extractLinks(html: String) -> Dictionary<String, String>? {
do {
let regex = try NSRegularExpression(pattern: "/<([a-z]*)\b[^>]*>(.*?)</\1>/i", options: [])
let nsString = html as NSString
let results = regex.matchesInString(html, options: [], range: NSMakeRange(0, nsString.length))
return results.map { nsString.substringWithRange($0.range)}
} catch let error as NSError {
print("invalid regex: \(error.localizedDescription)")
return nil
}
}
你的正則表達式代碼在哪裏? – matt
@matt:他們在等你寫它。 –
它非常糟糕。 – Rao