Using NSRegularExpression to extract URLs on the iPhone

后端 未结 5 623
耶瑟儿~
耶瑟儿~ 2020-11-30 09:16

I\'m using the following code on my iPhone app, taken from here to extract all URLs from striped .html code.

I\'m only being able to extract the first URL, but I nee

5条回答
  •  醉酒成梦
    2020-11-30 09:47

    With NSDataDetector using Swift :

    let types: NSTextCheckingType = .Link
    var error : NSError?
    
    let detector = NSDataDetector(types: types.rawValue, error: &error)        
    var matches = detector!.matchesInString(text, options: nil, range: NSMakeRange(0, count(text)))
    
    for match in matches {
       println(match.URL!)
    }
    

    Using Swift 2.0:

    let text = "http://www.google.com. http://www.bla.com"
    let types: NSTextCheckingType = .Link
    
    let detector = try? NSDataDetector(types: types.rawValue)
    
    guard let detect = detector else {
       return
    }
    
    let matches = detect.matchesInString(text, options: .ReportCompletion, range: NSMakeRange(0, text.characters.count))
    
    for match in matches {
       print(match.URL!)
    }
    

    Using Swift 3.0

    let text = "http://www.google.com. http://www.bla.com"
    let types: NSTextCheckingResult.CheckingType = .link
    
    let detector = try? NSDataDetector(types: types.rawValue)
    
    let matches = detector?.matches(in: text, options: .reportCompletion, range: NSMakeRange(0, text.characters.count))
    
    for match in matches! {
       print(match.url!)
    }
    

提交回复
热议问题