[categories] [index] [all (552)] [latest]
Two methods to be added to a NSString
category:
-[NSString captureRegex:]
- (NSString *)captureRegex:(NSString *)pattern {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionDotMatchesLineSeparators error:&error];
if(regex == nil) {
NSLog(@"-- %@", error);
return nil;
}
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
if(rangeOfFirstMatch.location == NSNotFound) return nil;
return [self substringWithRange:rangeOfFirstMatch];
}
-[NSString matchRegex:]
- (BOOL)matchRegex:(NSString *)pattern {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
if(regex == nil) {
NSLog(@"-- %@", error);
return NO;
}
NSUInteger n = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
return n == 1;
}