NSRegularExpression,指定区分大小写的匹配?

时间:2012-02-08 17:06:27

标签: ios iphone objective-c cocoa-touch nsregularexpression

是否有办法使用NSRegularExpression指定您要进行区分大小写的搜索?我试图在下面的文本中匹配大写TAG“ACL”。我使用的模式很简单:

// Pattern
[A-Z]+

// SearchText
<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>

// Code:
NSString *textBuffer = @"<td align=\"left\" nowrap><font face=\"courier, monospace\" size=\"-1\">ACL*</font></td>";
NSString *pattern = @"([A-Z]+)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSTextCheckingResult *result = [regExp firstMatchInString:textBuffer options:0 range:NSMakeRange(0, [textBuffer length])];
NSLog(@"OBJECT CLASS: %@", [textBuffer substringWithRange:[result range]]);

输出:(有案例 - 无意义我正在按预期获得第一个“td”,而我真正想要的是“ACL”

我知道NSRegularExpressionCaseInsensitive错了,我希望会有NSRegularExpressionCaseSensitive。还有一个flagOption ?(i),它还指定了一个不区分大小写的搜索,但同样没有任何案例敏感的搜索。我错过了什么?

1 个答案:

答案 0 :(得分:10)

区分大小写是默认值。不要把不敏感的旗帜放在那里。