2012-01-20 68 views
2

我正在做一个正则表达式以下行:NSRegularExpression不匹配

Table 'Joella VIII' 6-max Seat #4 is the button 

到目前为止,我有这样的:

self.tableDetailsRegex = [NSRegularExpression regularExpressionWithPattern:@"Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button" options:NSRegularExpressionAllowCommentsAndWhitespace error:nil]; 

if([self.tableDetailsRegex numberOfMatchesInString:line options:NSMatchingReportCompletion range:NSMakeRange(0, line.length)] == 1) 
{ 
    NSLog(@"%@", line); 
} 

所以,我的正则表达式为:

Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button 

而且我敢肯定,所选线路在某些时候来的,因为我打印所有行远一点在我的代码...

+0

FWIW,如果我复制/粘贴您的模式和测试字符串到我的工具,它确实匹配。相对于“if”的问题? – Seki

回答

2

您的问题出在您正在使用的选项中。从NSRegularExpression Class Reference,NSRegularExpressionAllowCommentsAndWhitespace表示正则表达式中的空格和任何东西#将被忽略。启用该选项,正则表达式就像这样:

Table'[A-Za-z0-9]*'[0-9]+-maxSeat 

你可能想为options传递0,所以,他们没有得到启用。

self.tableDetailsRegex = [NSRegularExpression regularExpressionWithPattern:@"Table '[A-Za-z0-9 ]*' [0-9]+-max Seat #[0-9]+ is the button" options:0 error:nil]; 
+0

谢谢,这工作 –

相关问题