我正在解析一个xml文件,我可以NSLog解析,但我的问题是我需要从这个“字符串”中获取图像url:
<p>
<a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg"><img class="alignnone size-thumbnail wp-image-81" title="ex4" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg" alt="" width="150" height="150" /></a>
<a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg"><img class="alignnone size-thumbnail wp-image-80" title="ex3" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg" alt="" width="150" height="150" /></a>
<a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg"><img class="alignnone size-thumbnail wp-image-79" title="ex2" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg" alt="" width="150" height="150" /></a>
<a href="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg"><img class="alignnone size-thumbnail wp-image-71" title="ex1" src="http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg" alt="" width="150" height="150" /></a>
</p>
对于普通代码抱歉:)
我用来提取url的是这段代码,但它不起作用:
NSRange start = [item.imageGallery rangeOfString:@"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/"];
NSRange end = [item.imageGallery rangeOfString:@"\" "];
int rangeLength = (int)(end.location - start.location);
NSString *hrefString = [[NSString alloc] initWithString:[item.imageGallery substringWithRange:NSMakeRange(start.location, rangeLength)]];
NSLog(@"image url = %@",hrefString);
答案 0 :(得分:7)
使用正则表达式:"src=\"([^\"]+)\""
以下是一些示例代码:
NSString *searchedString = @""
@"<p>"
@"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43.jpg\"><img class=\"alignnone size-thumbnail wp-image-81\" title=\"ex4\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
@"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32.jpg\"><img class=\"alignnone size-thumbnail wp-image-80\" title=\"ex3\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
@"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23.jpg\"><img class=\"alignnone size-thumbnail wp-image-79\" title=\"ex2\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
@"<a href=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12.jpg\"><img class=\"alignnone size-thumbnail wp-image-71\" title=\"ex1\" src=\"http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg\" alt=\"\" width=\"150\" height=\"150\" /></a>"
@"</p>";
NSRange rangeOfString = NSMakeRange(0, [searchedString length]);
//NSLog(@"searchedString: %@", searchedString);
NSString *pattern = @"src=\"([^\"]+)\"";
NSError* error = nil;
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matchs = [regex matchesInString:searchedString options:0 range:rangeOfString];
for (NSTextCheckingResult* match in matchs) {
NSLog(@"url: %@", [searchedString substringWithRange:[match rangeAtIndex:1]]);
}
NSLog输出:
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex43-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex32-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex23-150x150.jpg
url: http://www.bubblesurprise.com/WPRESS_APP/wp-content/uploads/2012/02/ex12-150x150.jpg
答案 1 :(得分:2)
在这里,我找到了你:
https://stackoverflow.com/a/5999294/1047258
答案中的代码:
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
NSArray* matches = [detector matchesInString:source options:0 range:NSMakeRange(0, [source length])];
然后处理URL:
for (NSTextCheckingResult *match in matches) {
NSURL *url = [match URL];
// do whatever you want with the url
}