2012-01-20 41 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

謝謝,這工作 –