简体   繁体   中英

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];

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM