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

发布于 2024-12-18 21:39:26 字数 1594 浏览 0 评论 0原文

我使用以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潦草背影 2024-12-25 21:39:26

在使用第二个扫描仪之前,将 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文