I have the following expression that should match the entire given word in case insensitive way.Quotes are part of the word so I check whether the word is preceded or followed by any quote. For example, the word “foo” shouldn’t match the text “foo’s”.
word = "foo"
pattern = re.compile(r'(?<![a-z\'])%s(?![a-z\'])' % word,flags=re.IGNORECASE)
The exception are triple quotes, if the word is inside(next to) the triple quotes it should match:
pattern.search("'''foo bar baz'''")
“foo” should be found this time but it doesn’t because the word is preceded by a quote.
1 Answer