从字符串优化中提取链接 [英] Extract links from string optimization
问题描述
我从网站获取数据(HTML字符串)。我想提取所有链接。我写函数(它有效),但它太慢了......
你能帮我优化一下吗?我可以使用哪些标准功能?
函数逻辑:在文本中找到http:.//sting,然后读取字符串(购买字符)直到我不会得到\。
extension String {
subscript(i:Int) - > Character {
return self [advance(self.startIndex,i)]
}
下标(i:Int) - >字符串{
返回字符串(self [i] as Character)
}
下标(r:Range< Int>) - > String {
返回substringWithRange(范围(start:advance(startIndex,r.startIndex),end:advance(startIndex,r.endIndex)))
}}
func extractAllLinks(text:String) - >数组< 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}
就像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.
这篇关于从字符串优化中提取链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!