在下面的代码中,我尝试使用正则表达式来提取文本文件的更下面的部分。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSISOLatin1StringEncoding];
NSLog(@"string length: %d", [string length]);
NSError *error = nil;
NSString *toMatch = @"\[Board\\t\"([0-9]?)\"]*\[Dealer\\t\"([NEWS])\"]*";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:toMatch
options:0 error:&error];
NSLog(@"length: %d", [toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@" %ud", numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])]){
// cannot make this work: NSRange trange =[match range];
// cannot make this work: NSLog(@"range %i,%i", trange );
NSString* tstring=[string substringWithRange:trange];
NSLog(@" %@", tstring );}
}
我NSRegularExpression
用来从以下摘录的文本中挑选信息。更具体地说,我需要每个板的Board
数量和Dealer
值(大约有 40 个板,我删除了列表中的几个不相关的行)。
[Board "1"]
[Dealer "N"]
[Vulnerable "None"]
[Deal "N:Q952.652.KJT4.95 T.KQT84.A865.J73 K8763.A7.Q.KQT84 AJ4.J93.9732.A62"]
[Scoring ""]
[Declarer ""]
[Contract ""]
[Board "2"]
[Dealer "E"]
[Vulnerable "NS"]
[Deal "E:K8542.3.4.AT7532 J76.K7.AT85.KQJ8 QT3.AJ84.KJ963.4 A9.QT9652.Q72.96"]
[Scoring ""]
[Declarer ""]
[Contract ""]
我正在为我的for
循环打印得到 gobbledygook。gobbledygook 至少有两个原因:我的正则表达式错误,或者我的for
循环错误。
控制台输出并gobbledygook
遵循。
2013-02-03 11:00:14.161 BridgeDuplicate[51867:11303] the window: <UIWindow: 0x956eac0; frame = (0 0; 768 1024); hidden = YES; layer = <UIWindowLayer: 0x956ebc0>>
2013-02-03 11:00:14.163 BridgeDuplicate[51867:11303] the rootViewController: <BSViewController: 0x7188220>
2013-02-03 11:00:14.166 BridgeDuplicate[51867:11303] viewDidLoad
2013-02-03 11:00:27.156 BridgeDuplicate[51867:11303] Succeeded! Received 303896 bytes of data
2013-02-03 11:00:27.158 BridgeDuplicate[51867:11303] string length: 303896
2013-02-03 11:00:27.164 BridgeDuplicate[51867:11303] length: 41
2013-02-03 11:00:27.205 BridgeDuplicate[51867:11303] 264765d
2013-02-03 11:00:27.205 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303] l
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.206 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303] ea
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303] d
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.207 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] a
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.208 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.209 BridgeDuplicate[51867:11303] "
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303] o
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303] e
2013-02-03 11:00:27.228 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.229 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303] e"
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303]
2013-02-03 11:00:27.230 BridgeDuplicate[51867:11303]