在iOS系统中,我需要检查对正则表达式字符串,如果通过,那么返回true
(例如),如果没有false
。我明白,我必须使用NSRegularExpression
类,但我无法弄清楚如何。
你应该阅读documentation。
这里有一个例子代码如何在一般做到这一点:
- (BOOL)checkString:(NSString *)string {
NSString *const expression = @"^\\d{3}[-]\\d{2}[-]\\d{4}$"; // insert yours
NSError *error = nil;
NSRegularExpression * const regExpr =
[NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult * const matchResult = [regExpr firstMatchInString:string
options:0 range:NSMakeRange(0, [string length])];
return matchResult ? YES : NO;
}