在Swift中, 要从HTML格式的String中截取出所有的img的所有图片url,要截取的url就要匹配url,需要用到正则表达式。
extension String {
/** 根据 正则表达式 截取字符串 - parameter regex: 正则表达式 - returns: 字符串数组 */
public func matchesForRegex(regex: String) -> [String]? {
do {
let regularExpression = try NSRegularExpression(pattern: regex,options: [])
let range = NSMakeRange(0,self.characters.count)
let results = regularExpression.matchesInString(self,options: [],range: range)
let string = self as NSString
return results.map { string.substringWithRange($0.range)}
} catch {
return nil
}
}
}
- 匹配img标签的正则表达式为:
<img\b(?=\s)(?=(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\ssrc=['"]([^"]*)['"]?)(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*"\s?\/?>
- 在swift中的正则表达式书写为:
let regex = "<img\\b(?=\\s)(?=(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"][^\\s>]*)*?\\ssrc=['\"]([^\"]*)['\"]?)(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"\\s]*)*\"\\s?\\/?>"
- 使用案例:
let string = "图片1<div><img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\"><br></div><div>图片2</div><div><img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\"><br></div><div><br></div><div>图片3</div><div><img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\"><br></div><div><br></div>"
// 截取img标签
let resultItems = string.matchesForRegex(regex)
- 截取得到的resultItems为:
["<img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\">","<img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\">","<img src=\"http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg\" alt=\"Gravatar\">"]
(http[^\s]+(jpg|jpeg|png|tiff)\b)
- 在Swift中书写为:
let regex = "(http[^\\s]+(jpg|jpeg|png|tiff)\\b)"
- 使用案例:
// 截取所有img url
let resultItems = string.matchesForRegex(regex)
- 结果为:
[“http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg“,“http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg“,“http://cms-bucket.nosdn.127.net/catchpic/C/C7/C7643B52B98D04979AA173C6D6219958.jpg“]