我有以下文字
<select name="username"><option value="177"> Bob1
</option><option value="221"> Bob2
</option><option value="227"> Bob3
</option><option value="164"> Bob4
</option><option value="271"> Bob5
</option><option value="137"> Bob6
</option><option value="105"> Bob7
</option><option value="285"> Bob8
</option><option value="281"> Bob9
</option><option value="265"> Bob10
</option></select>
我正在尝试使用NSScanner来获取选项值和选项标签中的名称。到目前为止,我有以下代码
for (int y = 1; y < 16; y++) {
NSScanner *scanner1 = [NSScanner scannerWithString:htmlsource];
[scanner1 scanUpToString:[NSString stringWithFormat:@"<option value=\""] intoString:NULL];
[scanner1 scanString:[NSString stringWithFormat:@"<option value=\""] intoString:NULL];
[scanner1 scanUpToString:@"\"" intoString:&result];
NSLog(@"%i",[scanner1 scanLocation]);
NSLog(result);
[scanner1 setScanLocation:([scanner1 scanLocation] - 18)];
[scanner1 scanUpToString:[NSString stringWithFormat:@"<option value=\"%@\">",result] intoString:NULL];
[scanner1 scanString:[NSString stringWithFormat:@"<option value=\"%@\">",result] intoString:NULL];
[scanner1 scanUpToString:@"</option>" intoString:&result];
//NSLog([NSString stringWithFormat:@"<option value=\"%@\">",result]);
NSLog(@"%i",[scanner1 scanLocation]);
NSLog(result);
}
这仅适用于第一个条目。我是在做错了还是我必须从它停止的地方开始扫描,如果是这样的话怎么样? 到目前为止的结果是..
2009-07-31 08:15:53.859 App1[1000:20b] 683
2009-07-31 08:15:53.860 App1[1000:20b] 177
2009-07-31 08:15:53.860 App1[1000:20b] 712
2009-07-31 08:15:53.860 App1[1000:20b] Bob1
2009-07-31 08:15:53.861 App1[1000:20b] 683
2009-07-31 08:15:53.861 App1[1000:20b] 177
2009-07-31 08:15:53.862 App1[1000:20b] 712
2009-07-31 08:15:53.862 App1[1000:20b] Bob1
答案 0 :(得分:1)
始终有RegexKitLite。
此版本将空白区域保留在<option>...</option>
:
NSString *regex = @"(?si)<option\\s+value\\s*=\\s*\"([^\"]*)\"[^>]*>(.*?)</option>";
NSArray *htmlOptionsArray = [htmlsource arrayOfCaptureComponentsMatchedByRegex:regex];
for(NSArray *parsedOptionArray in htmlOptionsArray) {
NSString *value = [parsedOptionArray objectAtIndex:1UL];
NSString *text = [parsedOptionArray objectAtIndex:2UL];
NSLog(@"Value: '%@', text: '%@'", value, text);
}
示例输出:
2009-07-31 04:20:23.692 so[35423:807] Value: '177', text: ' Bob1
'
2009-07-31 04:20:23.699 so[35423:807] Value: '221', text: ' Bob2
'
....
2009-07-31 04:20:23.725 so[35423:807] Value: '281', text: ' Bob9
'
2009-07-31 04:20:23.726 so[35423:807] Value: '265', text: ' Bob10
'
此版本剥离了选项文本周围的任何额外空白区域:
NSString *regex = @"(?si)<option\\s+value\\s*=\\s*\"([^\"]*)\"[^>]*>\\s*(.*?)\\s*</option>";
NSArray *htmlOptionsArray = [htmlsource arrayOfCaptureComponentsMatchedByRegex:regex];
for(NSArray *parsedOptionArray in htmlOptionsArray) {
NSString *value = [parsedOptionArray objectAtIndex:1UL];
NSString *text = [parsedOptionArray objectAtIndex:2UL];
NSLog(@"Value: '%@', text: '%@'", value, text);
}
示例输出:
2009-07-31 04:21:50.352 so[35436:807] Value: '177', text: 'Bob1'
2009-07-31 04:21:50.354 so[35436:807] Value: '221', text: 'Bob2'
...
2009-07-31 04:21:50.359 so[35436:807] Value: '281', text: 'Bob9'
2009-07-31 04:21:50.359 so[35436:807] Value: '265', text: 'Bob10'
答案 1 :(得分:0)
如果它是格式良好的XML,那么你可能最好使用像NSXML这样的XML解析器为你做繁重的工作:
另一个问题是您正在将扫描仪重置回选项值字符串的开头,因此当您重新扫描时,您将从每次停止的相同位置开始。当然,重点是不要这样做,并继续前进?
[scanner1 setScanLocation:([scanner1 scanLocation] - 18)];
如果您对该行发表评论,它会神奇地开始工作吗?