且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从字符串优化中提取链接

更新时间:2023-02-23 11:16:49

就像AdamPro13上面说的那样使用 NSDataDetector 你可以轻松搞定所有网址,请参阅以下代码:

  let text =http://www.google.com。 http://www.bla.com
让类型: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)))

匹配匹配项{
println(match.URL!)
}

输出:

  http://www.google.com 
http://www.bla.com




更新为Swift 2.0




  let text =http://www.google.com。 http://www.bla.com
让类型:NSTextCheckingType = .Link

让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) ))

匹配比赛{
print(match.URL!)
}

在上述情况下,请记住使用 guard 语句,它必须在函数或循环中。



我希望这有帮助。


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 functions I can use? Function logic: find "http:.//" sting in text, and then read string (buy char) until I will not get "\"".

extension String {

subscript (i: Int) -> Character {
    return self[advance(self.startIndex, i)]
}

subscript (i: Int) -> String {
    return String(self[i] as Character)
}

subscript (r: Range<Int>) -> String {
    return substringWithRange(Range(start: advance(startIndex, r.startIndex), end: advance(startIndex, r.endIndex)))
}}



func extractAllLinks(text:String) -> Array<String>{
var stringArray = Array<String>()
var find = "http://" as String

for (var i = countElements(find); i<countElements(text); i++)
{
    var ch:Character = text[i - Int(countElements(find))]
    if (ch == find[0])
    {
        var j = 0
        while (ch == find[j])
        {
            var ch2:Character = find[j]
            if(countElements(find)-1 == j)
            {
                break
            }
            j++
            i++
            ch = text[i - Int(countElements(find))]
        }

        i -= j
        if (j == (countElements(find)-1))
        {
            var str = ""
            for (; text[i - Int(countElements(find))] != "\""; i++)
            {
                str += text[i - Int(countElements(find))]
            }
            stringArray.append(str)
        }

    }
}
return stringArray}

Like AdamPro13 said above using NSDataDetector you can easily get all the URLs, see it the following code :

let text = "http://www.google.com. http://www.bla.com"
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!)
}

It outputs :

http://www.google.com
http://www.bla.com

Updated to 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!)
}

Remember to use the guard statement in the above case it must be inside a function or loop.

I hope this help.