本文介绍了使用NSScanner查找短语的下一个匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

。如何查找Period1的第一场比赛和Period2的第二场比赛?

in my code. How can I look for the first match on "Period1" and the second match on "Period2"?

谢谢!

推荐答案

在使用第二个扫描程序之前,将 scanLocation 设置为超出短语第一个实例的开头,如下所示:

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)];

编辑:只是一个想法 - 你为什么要使用两个扫描仪?只需像这样使用一台扫描仪:

Just a thought - why are you using two scanners at all? Just use the one scanner like so:

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);

这篇关于使用NSScanner查找短语的下一个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 22:42