【发布时间】:2015-03-07 14:52:24
【问题描述】:
我们使用下面的NSPredicate 从CoreData 获取实体:
[NSPredicate predicateWithFormat:@"name MATCHES[cd] %@", [NSString stringWithFormat:@"^%@.*|.*[-\\s.@]%@.*", userInputtedKey, userInputtedKey]];
如果userInputtedKey 仅包含例如字母和数字,则一切正常。但是,如果userInputtedKey 本身包含正则表达式的保留符号,则匹配将失败甚至崩溃。
例如userInputtedKey 为@"(" 会导致崩溃:
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法进行正则表达式匹配, 原因:无法打开模式 U_REGEX_MISMATCHED_PAREN (string Yu, 模式 ^(.|.[-\s.@](.*, case 1, canon 2)'
userInputtedKey的内容不受我控制;它可以是用户输入的任何内容。是否可以让系统不解析userInputtedKey中的保留符号,只需将userInputtedKey作为一个整体进行匹配?比如引用''?
【问题讨论】: