NSString *pattern = @"(?<=<td class=\"bigblod\">).*?(?=</td>)"; 2. 3. NSString *pattern1 = @"<img src=\"http://www.weather.com.cn/m2/i/icon_weather/50x36/.*?gif\">"; 4. 5. NSURL *URL = [NSURL URLWithString:@"http://www.weather.com.cn/weather/101010100.shtml"]; 6. NSString *string = [NSString stringWithContentsOfURL:URL encoding:NSUTF8StringEncoding error:nil]; 7. 8. NSRange range = NSMakeRange(0, string.length); 9. 10. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionDotMatchesLineSeparators error:nil]; 11. 12. NSArray *matches = [regex matchesInString:string options:0 range:range]; 13. 14. NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options:NSRegularExpressionAnchorsMatchLines|NSRegularExpressionDotMatchesLineSeparators error:nil]; 15. 16. NSArray *matches1 = [regex1 matchesInString:string options:0 range:range]; 17. 18. 19. NSMutableString *s = [[NSMutableString alloc]init]; 20. [s appendString: @"<meta charset=\"utf-8\">"]; 21. 22. int i = 0; 23. for (NSTextCheckingResult *match in matches) { 24. NSTextCheckingResult *match1 = [matches1 objectAtIndex:i]; 25. i ++; 26. 27. NSRange matchRange = [match range]; 28. NSRange matchRange1 = [match1 range]; 29. NSLog(@"Reg:%@",[string substringWithRange:matchRange]); 30. 31. [s appendString:[string substringWithRange:matchRange1]]; 32. [s appendString:@"</br>"]; 33. 34. [s appendString:[string substringWithRange:matchRange]]; 35. [s appendString:@"</br>"]; 36. 37. 38. } 39. 40. [self.webview1 loadHTMLString:s baseURL:nil];
ios项目开发(天气预报项目):使用正则获取 weather.com.cn站点信息
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:acm省赛选拔组队赛经验谈
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python实战-天气预报项目-获取天气信息
Python实战项目
Python 实现项目