2017-02-16 54 views
0

我做了一个RSS阅读器,我也试图显示一个预览图像。 下面是我用什么来获取图像和不工作的唯一的事情是我需要帮助正从“ST画廊”的形象创造了良好的模式类的travelator.ro网站的模式需要帮助创建一个正则表达式模式获取图像

if item?.content != nil { 

     print("works until here") 
     let htmlContent = item!.content as NSString 
     var imageSource = "" 

     let rangeOfString = NSMakeRange(0, htmlContent.length) 
     let regex = try! NSRegularExpression(pattern: "(http[^\\s]+(jpg|jpeg|png|tiff)\\b)", options: .caseInsensitive) 

     if htmlContent.length > 0 { 
      let match = regex.firstMatch(in: htmlContent as String, options: [], range: rangeOfString) 

      if match != nil { 
       let imageURL = htmlContent.substring(with: (match!.rangeAt(2))) as NSString 
       print(imageURL) 

       if NSString(string: imageURL.lowercased).range(of: "feedburner").location == NSNotFound { 
        imageSource = imageURL as String 
       } 
      } 
     } 

     if imageSource != "" { 
      cell.itemImageView.setImageWith(NSURL(string: imageSource) as URL!, placeholderImage: UIImage(named: "thumbnail")) 
     }else { 
      cell.itemImageView.image = UIImage(named: "thumbnail") 
     } 
    } 

enter image description here

非常感谢提前。 :)

+0

使用HTML解析库。 [正则表达式无法解析HTML。](http://stackoverflow.com/a/1732454/3141234)正则表达式可识别一组常规语言。 HTML是一种上下文无关的语言,在Chomsky Hierarchy上更高。正则表达式无法识别上下文无关语言。 – Alexander

回答

0

Regular expressions can't parse HTML.正则表达式会识别一组正则语言。 HTML是一种上下文无关的语言,在Chomsky Hierarchy上更高。正则表达式无法识别上下文无关语言。

您需要使用更复杂的解析器。 HTML解析库已经做到了这一点,我建议你看看使用其中之一。

+0

我明白了。例如,奇怪的是,()适用于texanerin.com。 – perte

+0

@perte您可以制作适用于特定示例的正则表达式,但不适用于一般的语言。 – Alexander

+0

这是我最初的问题。我似乎无法为travelator.ro网站写一个好的正则表达式。 – perte