次のコードでは、正規表現を使用して、テキスト ファイルのさらに下の部分を抽出しようとしています。
- (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 には少なくとも 2 つの理由があります。正規表現が間違っているか、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]