I'd like to use a regex to find an exact string, but not if it's part of a comment, as designated by //
.
So for example, in the string:
hello apple apples // eat an apple
It should match the first apple but not the second or third.
So, I think the regex would be something like this. It would find the string with word breaks around it, but not if the //
is behind it:
(?<!\/\/)\bapple\b
The problem with negative look-behind in this case is that it only looks immediately next to the word. I'd need it to look farther back, to make sure the comment symbol does not exist earlier in the string.
(?<!//.*)\bapple\b
with Python regex package could also work. – Hyphenated