2011-11-30 72 views
2

我使用下面的代碼找到的某行代碼在我的HTML文件:查找短語的下一場比賽與NSScanner

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」上的第二個匹配?

謝謝!

+0

你能後的NSString的值* –

+0

頁它的「requestTimetableURL」,這是http://www.dhsb.org/index.phtml?d=201435 – pixelbitlabs

回答

0

使用第二掃描儀前,設置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);