I am trying to get from html string using regex which I am currently working on was this :
extension String {
func regex (pattern: String) -> [String] {
do {
let regex = try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions(rawValue: 0))
let nsstr = self as NSString
let all = NSRange(location: 0, length: nsstr.length)
var matches : [String] = [String]()
regex.enumerateMatchesInString(self, options: NSMatchingOptions(rawValue: 0), range: all) {
(result : NSTextCheckingResult?, _, _) in
if let r = result {
let result = nsstr.substringWithRange(r.range) as String
matches.append(result)
}
}
return matches
} catch {
return [String]()
}
}
And the pattern is : <img[^>]+src\\s*=\\s*['\']([^'\"]+)['\"][^>]*>
I still can't get the image url from it which mean it return me empty array.Actually my html string include one image.I don't want to useUIWebView
because of UITableView
resizing problem.So,I need to fetch the image url out of html and show it in UIImageView using AlamofireImage.
Any Help?It was just one url that i need to fetch.
Here is my tag :
<img src="https://en.wikipedia.org/wiki/File:BH_LMC.png"/>
To :
https://en.wikipedia.org/wiki/File:BH_LMC.png
Just a slight tweak to the pattern give me this:
returns an array with the match.
Description
This regular expression will do the following:
Example
Live Demo
https://regex101.com/r/qW9nG8/1
Sample text
Note the difficult edge case in the first line where we are looking for a specific droid.
Sample Matches
Explanation
This is the regex pattern you should use:
Translation:
Regex function:
Usage:
Note: