Karolina
Karolina

Reputation: 55

Excluding 2 words from search - match /word/ but not /a word/

I need to search my corpus for the word game but I would like to specify the search to exclude one of the uses of the word: a game. So actually I need to exclude the string a+space+game

I was trying to compose the regex search string, but unsuccessfully: \bgame\b^(?!.*?[a gam]).*

I'm sorry if I'm asking a question that has already been answered before. The thing is I'm not sure what to look for to get the answer.

Upvotes: 2

Views: 58

Answers (1)

cs95
cs95

Reputation: 402813

game that is not preceded by a_? You can use a negative lookbehind.

(?<!a\s)game

Demo: https://regex101.com/r/2PQi1B/2

A more accurate version of this as suggested by Wiktor Stribiżew (to explicitly match word boundaries for edge cases):

r'(?<!\ba\s)\bgame\b 

Upvotes: 3

Related Questions