0

I made an RSS reader and I'm trying to get to display a preview image too. Here's what I'm using to get the image and the only thing that's not working is the pattern

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")
        }
    }

I need help creating a good pattern for getting the image from "st-gallery" class of the travelator.ro website. enter image description here

Many thanks in advance. :)

perte
  • 271
  • 2
  • 3
  • 16
  • Use an HTML parsing library. [Regular expressions can't parse HTML.](http://stackoverflow.com/a/1732454/3141234) Regular expressions recognize the set of Regular Languages. HTML is a context-free language, which is higher on the Chomsky Hierarchy. Regular expressions can't recognize context free languages. – Alexander Feb 17 '17 at 00:05

1 Answers1

0

Regular expressions can't parse HTML. Regular expressions recognize the set of Regular Languages. HTML is a context-free language, which is higher on the Chomsky Hierarchy. Regular expressions can't recognize context free languages.

You would need to use a more complicated parser. HTML parsing libraries have done this, I suggest you look at using one of those.

Community
  • 1
  • 1
Alexander
  • 59,041
  • 12
  • 98
  • 151