Reputation: 8236
I've tried the following to exclude the words 'and' and 'the' from a regex search but it doesn't seem to be working. Any idea what I'm doing wrong?
NSString *pattern = [NSString stringWithFormat:@"\\b%@\\b\\b(?!.*\\b(?:and|the)\\b)", word];
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options: NSRegularExpressionCaseInsensitive error:nil];
Upvotes: 0
Views: 302
Reputation: 4486
Seems like Objective C does support negative lookbehinds.. so I'm going to link you two excellent posts about negative lookbehinds.
http://www.codinghorror.com/blog/2005/10/excluding-matches-with-regular-expressions.html (by Jeff Atwood)
http://www.regular-expressions.info/lookaround.html (second link on the above blog post)
Upvotes: 1