使用 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”上的第二场比赛?
谢谢!
I am using the following code to find a certain line of code in my HTML file:
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);
You will notice that both the strings to find are the same. This is because there is nothing to actually distinguish between the two lines of code. There are two matches of:
<P align=center><STRONG><FONT color=#c00000>
in my code. How can I look for the first match on "Period1" and the second match on "Period2"?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用第二个扫描仪之前,将
scanLocation
设置为超出短语第一个实例的开头,如下所示:[htmlScanner2 setScanLocation:(htmlScanner1.scanLocation + 1)];
编辑:只是一个想法 - 你为什么要使用两台扫描仪?只需使用一台扫描仪,如下所示:
Before using the second scanner, set the
scanLocation
to beyond the start of the first instance of the phrase like so:[htmlScanner2 setScanLocation:(htmlScanner1.scanLocation + 1)];
EDIT: Just a thought - why are you using two scanners at all? Just use the one scanner like so: