ios项目开发(天气预报项目):使用正则获取 weather.com.cn网站信息

NSString *pattern = @"(?<=).*?(?=)";
2.
3.    NSString *pattern1 = @"";
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: @""];
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:@"
"]; 33. 34. [s appendString:[string substringWithRange:matchRange]]; 35. [s appendString:@"
"]; 36. 37. 38. } 39. 40. [self.webview1 loadHTMLString:s baseURL:nil];

你可能感兴趣的:(ios)