Extract links from string optimization

前端 未结 7 991
予麋鹿
予麋鹿 2021-01-03 05:21

I get data (HTML string) from website. I want to extract all links. I write function (it works), but it is so slow...

Can you help me to optimize it? What standard

相关标签:
7条回答
  • 2021-01-03 05:43

    And that is the answer for Swift 5.0

    let text = "http://www.google.com. http://www.bla.com"
    
    func checkForUrls(text: String) -> [URL] {
        let types: NSTextCheckingResult.CheckingType = .link
    
        do {
            let detector = try NSDataDetector(types: types.rawValue)
    
            let matches = detector.matches(in: text, options: .reportCompletion, range: NSMakeRange(0, text.count))
        
            return matches.compactMap({$0.url})
        } catch let error {
            debugPrint(error.localizedDescription)
        }
    
        return []
    }
    
    checkForUrls(text: text)
    
    0 讨论(0)
提交回复
热议问题