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

4

使用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。此外,还有一个标志选项?(i)也指定了不区分大小写的搜索,但是没有找到区分大小写的选项。我错过了什么吗?

1个回答

12

默认情况下区分大小写,不要加入不区分大小写的标志。


1
谢谢,你可以用0代替NSRegularExpressionCaseInsensitive,就能正常工作了。只是有点奇怪没有一个专门的选项来处理这个。 - fuzzygoat
@fuzzygoat 没有这样的选项,因为区分大小写的搜索速度更快,是默认选项。 - Costique
在Swift中的示例:NSRegularExpression(pattern: "^@[a-z]+", options: NSRegularExpressionOptions(rawValue: 0)) - Bartłomiej Semańczyk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接