iphone - 使用NSScanner查找短语的下一个匹配项

标签 iphone objective-c ios xcode nsscanner

我正在使用以下代码在HTML文件中查找特定的代码行:

NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"];
NSLog(@"Loaded Timetable");
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL 
                                              encoding:NSASCIIStringEncoding
                                                 error:&error];

[webView loadHTMLString:page baseURL:requestTimetableURL];

NSString* Period1;

NSScanner *htmlScanner =  [NSScanner scannerWithString:page];

[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1];

period1label.text= Period1;

NSLog(@"Collected Period 1 Data: %@", Period1);

NSScanner *htmlScanner2 =  [NSScanner scannerWithString:page];

NSString* Period2;

[htmlScanner2 scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner2 scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner2 scanUpToString:@"</FONT>" intoString:&Period2];

period2label.text= Period2;

NSLog(@"Collected Period 2 Data: %@", Period2);

您会注意到,要查找的两个字符串是相同的。这是因为实际上没有什么可区分两行代码。有以下两个匹配项:
<P align=center><STRONG><FONT color=#c00000>

在我的代码中。如何在“Period1”上寻找第一场比赛,在“Period2”上寻找第二场比赛?

谢谢!

最佳答案

在使用第二台扫描仪之前,将scanLocation设置为该短语的第一个实例的开头之外,如下所示:
[htmlScanner2 setScanLocation:(htmlScanner1.scanLocation + 1)];
编辑:只是一个想法-为什么要使用两个扫描仪?像这样使用一台扫描仪:

NSURL *requestTimetableURL = [NSURL URLWithString:@"http://www.dhsb.org/index.phtml?d=201435"];
NSLog(@"Loaded Timetable");
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestTimetableURL 
                                              encoding:NSASCIIStringEncoding
                                                 error:&error];

[webView loadHTMLString:page baseURL:requestTimetableURL];

NSString* Period1;

NSScanner *htmlScanner =  [NSScanner scannerWithString:page];

[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period1];

period1label.text= Period1;

NSLog(@"Collected Period 1 Data: %@", Period1);

NSString* Period2;

[htmlScanner scanUpToString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanString:@"<P align=center><STRONG><FONT color=#c00000>" intoString:NULL];
[htmlScanner scanUpToString:@"</FONT>" intoString:&Period2];

period2label.text= Period2;

NSLog(@"Collected Period 2 Data: %@", Period2);

关于iphone - 使用NSScanner查找短语的下一个匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8325196/

相关文章:

iphone - 如何禁用 UIWebview 水平滚动?

ios - preformSegue 后编辑导航栏

android - Android 中蓝牙 LE 设备的最大绑定(bind)数是多少,即 'Paired Devices'?

ios - Swift 闭包 : Cannot convert return expression of type '()' to return type 'LiveSearchResponse?'

ios - 当用户点击带有 iOS Swift 的推送通知时,在特定 View 中打开应用程序

iphone - 随机数不重复

objective-c - UITableview 中没有正确的背景颜色

iphone - 图像未显示在 Interface Builder/iPhone 的 UIImageView 中

javascript - 数据 :uri for ical does not work on Android or iPhone

iphone - 加密和解密 Sqlite 文件(使用核心数据)