Objective c 来自regex和capture group的gobbledygook

Objective c 来自regex和capture group的gobbledygook,objective-c,regex,Objective C,Regex,在下面的代码中,我试图使用正则表达式提取下面文本文件的部分 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); NSString *string = [[NSString alloc] initWithData:receivedData encoding:NSI

在下面的代码中,我试图使用正则表达式提取下面文本文件的部分

- (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
从以下摘录的文本中挑选信息。更具体地说,我需要每个板的
编号和
经销商
值(大约有40个板,我删除了列表中几个不相关的行)

我正在为我的
循环打印输出获取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]  
2013-02-03 11:00:14.161复制[51867:11303]窗口:
2013-02-03 11:00:14.163复制[51867:11303]根视图控制器:
2013-02-03 11:00:14.166 BridgeDuplicate[51867:11303]viewDidLoad
2013-02-03 11:00:27.156复制[51867:11303]成功!接收到303896字节的数据
2013-02-03 11:00:27.158 BridgeDuplicate[51867:11303]字符串长度:303896
2013-02-03 11:00:27.164桥梁复制[51867:11303]长度:41
2013-02-03 11:00:27.205桥梁副本[51867:11303]264765d
2013-02-03 11:00:27.205重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]l
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.206重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]ea
2013-02-03 11:00:27.207重复[51867:11303]d
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.207重复[51867:11303]
2013-02-03 11:00:27.208桥牌副本[51867:11303]e
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208桥牌副本[51867:11303]a
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208重复[51867:11303]
2013-02-03 11:00:27.208桥牌副本[51867:11303]e
2013-02-03 11:00:27.209重复[51867:11303]
2013-02-03 11:00:27.209重复[51867:11303]
2013-02-03 11:00:27.209重复[51867:11303]
2013-02-03 11:00:27.209重复[51867:11303]
2013-02-03 11:00:27.209重复[51867:11303]
2013-02-03 11:00:27.209桥梁复制[51867:11303]
2013-02-03 11:00:27.228重复[51867:11303]
2013-02-03 11:00:27.228桥梁副本[51867:11303]o
2013-02-03 11:00:27.228重复[51867:11303]
2013-02-03 11:00:27.228重复[51867:11303]
2013-02-03 11:00:27.228桥梁副本[51867:11303]e
2013-02-03 11:00:27.228重复[51867:11303]
2013-02-03 11:00:27.229重复[51867:11303]
2013-02-03 11:00:27.229重复[51867:11303]
2013-02-03 11:00:27.229重复[51867:11303]
2013-02-03 11:00:27.229重复[51867:11303]
2013-02-03 11:00:27.229重复[51867:11303]
2013-02-03 11:00:27.230桥牌副本[51867:11303]e“
2013-02-03 11:00:27.230重复[51867:11303]
2013-02-03 11:00:27.230重复[51867:11303]

我怀疑您误解了
NSTextCheckingResult
的工作原理,但可能更重要的是,您的模式存在一些问题。以下代码应该是说明性的:

NSString *string = @"[Board\t\"1\"]\n[Dealer\t\"N\"]\n";
NSLog(@"string length: %lu", (unsigned long)[string length]);
NSError *error = nil;
NSString *toMatch = @"\\[Board\\t\"([0-9]?)\"\\].*\\n\\[Dealer\\t\"([NEWS])\"\\].*";
NSRegularExpression *regex = [NSRegularExpression  regularExpressionWithPattern:toMatch options:0 error:&error];
NSLog(@"length: %lu", (unsigned long)[toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"number of matches: %lu", (unsigned long)numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])])
{
    NSLog(@"Number of ranges in match: %lu", match.numberOfRanges);
    for (NSUInteger i = 0; i < match.numberOfRanges; ++i)
    {
        NSRange matchedRange = [match rangeAtIndex: i];
        NSString* tstring = [string substringWithRange: matchedRange];
        NSLog(@"range %lu string: %@", (unsigned long)i, tstring);
    }
}
这里要知道的是,你在为一场比赛射击,而这场比赛有多个射程。每个成功匹配都至少有一个范围:由整个模式匹配的整个字符串的范围(这不是您在这里感兴趣的内容)。基于括号的捕获组将出现在0以外的索引处,如以下代码所示

转义规则有点麻烦——在NSString中有转义规则,然后是转义正则表达式的规则。它们是如何相互作用的可能并不明显,但我在这里提出的模式似乎符合你的要求

编辑:

以下是另一个直接从您的URL提取并成功匹配的版本:

NSError* error = nil;
NSString* string = [NSString stringWithContentsOfURL: [NSURL URLWithString: @"http://www.atlantaduplicatebridgeclub.com/scorepost/2013/01/20130126ana.pbn"]
                                            encoding: NSUTF8StringEncoding error: &error];
NSLog(@"string length: %lu", (unsigned long)[string length]);
NSString *toMatch = @"\\[Board\\s*\"([0-9]?)\"\\].*\\[Dealer\\s*\"([NEWS])\"\\]";
NSRegularExpression *regex = [NSRegularExpression  regularExpressionWithPattern:toMatch options:NSRegularExpressionDotMatchesLineSeparators error:&error];
NSLog(@"pattern length: %lu", (unsigned long)[toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"number of matches: %lu", (unsigned long)numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:NSRegularExpressionDotMatchesLineSeparators range:NSMakeRange(0, [string length])])
{
    NSLog(@"Number of ranges in match: %lu", match.numberOfRanges);
    for (NSUInteger i = 0; i < match.numberOfRanges; ++i)
    {
        NSRange matchedRange = [match rangeAtIndex: i];
        NSString* tstring = [string substringWithRange: matchedRange];
        NSLog(@"range %lu string: %@", (unsigned long)i, tstring);
    }
}
NSError*error=nil;
NSString*string=[NSString stringWithContentsOfURL:[NSURL URLWithString:@]http://www.atlantaduplicatebridgeclub.com/scorepost/2013/01/20130126ana.pbn"]
编码:NSUTF8StringEncoding错误:&错误];
NSLog(@“字符串长度:%lu”,(无符号长)[字符串长度];
NSString*toMatch=@“\\[Board\\s*\”([0-9]?)\”\\\].\\\\[Dealer\\s*\”([NEWS])\“\\]”;
NSRegularExpression*regex=[NSRegularExpression regular expressionwithpattern:toMatch选项:NSRegularExpressionDotMatchesLineSeparators错误:&error];
NSLog(@“模式长度:%lu”,(无符号长)[toMatch length]);
NSUInteger numberOfMatches=[regex NumberOfMatchesInstalling:string选项:0范围:NSMakeRange(0,[string length]);
NSLog(@“匹配数:%lu”,(无符号长)numberOfMatches);
对于(NSTextCheckInResult*match in[regex MatchesInstalling:字符串选项:NSRegularExpressionDotMatchesLineSeparators范围:NSMakeRange(0,[字符串长度])中的匹配)
{
NSLog(@“匹配中的范围数:%lu”,match.numberOfRanges);
对于(整数i=0;i2013-02-03 12:16:41.112 RegExTest[72290:303] string length: 25
2013-02-03 12:16:43.889 RegExTest[72290:303] length: 49
2013-02-03 12:16:43.889 RegExTest[72290:303] number of matches: 1
2013-02-03 12:16:43.890 RegExTest[72290:303] Number of ranges in match: 3
2013-02-03 12:16:43.890 RegExTest[72290:303] range 0 string: [Board "1"]
[Dealer "N"]
2013-02-03 12:16:43.890 RegExTest[72290:303] range 1 string: 1
2013-02-03 12:16:43.890 RegExTest[72290:303] range 2 string: N
NSError* error = nil;
NSString* string = [NSString stringWithContentsOfURL: [NSURL URLWithString: @"http://www.atlantaduplicatebridgeclub.com/scorepost/2013/01/20130126ana.pbn"]
                                            encoding: NSUTF8StringEncoding error: &error];
NSLog(@"string length: %lu", (unsigned long)[string length]);
NSString *toMatch = @"\\[Board\\s*\"([0-9]?)\"\\].*\\[Dealer\\s*\"([NEWS])\"\\]";
NSRegularExpression *regex = [NSRegularExpression  regularExpressionWithPattern:toMatch options:NSRegularExpressionDotMatchesLineSeparators error:&error];
NSLog(@"pattern length: %lu", (unsigned long)[toMatch length]);
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];
NSLog(@"number of matches: %lu", (unsigned long)numberOfMatches);
for (NSTextCheckingResult* match in [regex matchesInString:string options:NSRegularExpressionDotMatchesLineSeparators range:NSMakeRange(0, [string length])])
{
    NSLog(@"Number of ranges in match: %lu", match.numberOfRanges);
    for (NSUInteger i = 0; i < match.numberOfRanges; ++i)
    {
        NSRange matchedRange = [match rangeAtIndex: i];
        NSString* tstring = [string substringWithRange: matchedRange];
        NSLog(@"range %lu string: %@", (unsigned long)i, tstring);
    }
}