0

I wrote simple method for checking if given array has at least one string, that matches provided pattern... but something is missing and not sure how to limit positive results only to full words, not just first substring that fits pattern.

+ (BOOL)hasWordsWithPattern:(NSString *)pattern inWords:(NSArray *)words{
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                            options:NSRegularExpressionCaseInsensitive
                                                                              error:nil];
for (NSString *s in words) {
    if ([expression matchesInString:s
                            options:0
                              range:NSMakeRange(0, s.length)]) {
        NSLog(@"there is a match!");
        return YES;
    }
}
NSLog(@"sorry, no match found!");
return NO;

}

raistlin
  • 4,298
  • 5
  • 32
  • 46

1 Answers1

0

Silly me, there is easier way to do that :) based on https://stackoverflow.com/a/5777016/1015049

+ (BOOL)hasWordsWithPattern:(NSString *)pattern inWords:(NSArray *)words{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];

for (NSString *s in words) {
    if ([predicate evaluateWithObject:s]) {
        NSLog(@"there is a match!");
        return YES;
    }
}
NSLog(@"sorry, no match found!");
return NO;

}

Community
  • 1
  • 1
raistlin
  • 4,298
  • 5
  • 32
  • 46