I'm assuming that the |
in your regex was supposed to mean "or." It doesn't: within brackets, no "or" is required. Your |
refers to the actual character |
.
This regular expression should do the trick about 98% of the time, maybe more:
\v(\/\/[^\n]*|\/\*(\_[^*]|\*\_[^/])*)@<!<[A-Z_]{2,}>
It uses positive lookbehind to make sure that there is no //
preceding the string in the same line and no /*
preceding it that is not followed by a */
. It fails in the following case:
if (string == "/*") { // Looks like the start of a block comment
return CONSTANT; // Won't be highlighted
}
If you want better results than this (that is, if you're worried that you'll obsess over the bug whenever you run into it) you could make this more sophisticated. How sophisticated depends on your language. In JavaScript, for example, you will need to worry about regex literals as well as strings:
// Looks like a comment after the "//" in the regex:
if (/\//.test(string)) return CONSTANT; // Won't be highlighted
If you want an idea of how complicated a regex to match a regex is, look at my answer here.