Smikey
Smikey

Reputation: 8236

Regex - excluding a word from a search

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

Answers (1)

Seth Malaki
Seth Malaki

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

Related Questions