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;

}

1 Answer 1

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;

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.