In iOS I need to check string against regular expression and if it passes then to return true
(for example), if not false
. I understand that I have to use NSRegularExpression
class, but I can not figure out how.
You should read documentation.
Here is an example code how to do this in general:
- (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;
}