使用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)
也指定了不区分大小写的搜索,但是没有找到区分大小写的选项。我错过了什么吗?
NSRegularExpression(pattern: "^@[a-z]+", options: NSRegularExpressionOptions(rawValue: 0))
。 - Bartłomiej Semańczyk