【问题标题】:NSRegularExpression, specify case-sensitive match?NSRegularExpression,指定区分大小写匹配?
【发布时间】:2012-03-01 04:01:18
【问题描述】:

有没有办法使用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) 也指定了不区分大小写的搜索,但同样没有区分大小写。我错过了什么?

【问题讨论】:

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


    【解决方案1】:

    区分大小写是默认设置。不要把不敏感的标志放在那里。

    【讨论】:

    • 谢谢,如果将 NSRegularExpressionCaseInsensitive 替换为 0,它确实有效,只是奇怪的是没有专门的选项。
    • @fuzzygoat 没有这样的选项,因为区分大小写的搜索速度更快,是默认选项。
    • @fuzzygoat 在 Swift 中的示例:NSRegularExpression(pattern: "^@[a-z]+", options: NSRegularExpressionOptions(rawValue: 0))
    猜你喜欢
    • 2022-06-13
    • 2015-01-05
    • 2017-11-14
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多