Find next match of a phrase with NSScanner

夙愿已清 提交于 2019-12-30 11:09:05

问题


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!


回答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:

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


来源:https://stackoverflow.com/questions/8325196/find-next-match-of-a-phrase-with-nsscanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!