Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从RSS提要获取img url_Ios_Regex_Swift_Rss - Fatal编程技术网

Ios 从RSS提要获取img url

Ios 从RSS提要获取img url,ios,regex,swift,rss,Ios,Regex,Swift,Rss,我希望能够从一段字符串中检索img url 以下是我试图检索的img URL示例: <p><img width="357" height="500" src="http://images.sgcafe.net/2015/05/OVA1-357x500.jpg" class="attachment- medium wp-post-image" alt="OVA1" /> 您应该尽可能多地使用src属性中的字符。您可以使用*?: var regex: NSR

我希望能够从一段字符串中检索img url

以下是我试图检索的img URL示例:

<p><img width="357" height="500" src="http://images.sgcafe.net/2015/05/OVA1-357x500.jpg" class="attachment-         medium wp-post-image" alt="OVA1" />

您应该尽可能多地使用
src
属性中的字符。您可以使用
*?

var regex: NSRegularExpression = NSRegularExpression(pattern: "<img.*?src=\"([^\"]*)\"", options: .CaseInsensitive, error: nil)!
在您的呼叫代码中:

var result = "<img.*?src=\"([^\"]*)\"".firstMatchIn(elementString, atRangeIndex: 1)
var结果=”
要确保
与换行符匹配,请使用
选项:NSRegularExpressionOptions.dotmatcheslineseparator

import Foundation

extension String {
    func firstMatchIn(string: NSString!, atRangeIndex: Int!) -> String {
        var error : NSError?
        let re = NSRegularExpression(pattern: self, options: .CaseInsensitive, error: &error)
        let match = re.firstMatchInString(string, options: .WithoutAnchoringBounds, range: NSMakeRange(0, string.length))
        return string.substringWithRange(match.rangeAtIndex(atRangeIndex))
    }
}
var result = "<img.*?src=\"([^\"]*)\"".firstMatchIn(elementString, atRangeIndex: 1)