I am trying to add some custom highlighting to my Vim. But my regex matching is not working correctly.
let ignoreHiInComments = '^\s*\(\/\/.*\)\@<!' "Ignore highlighting in C++ comments
call matchadd('UserTypes', ignoreHiInComments.'\zs\w\+\ze::.\+')
This will do the following highlighting:
myclass::myclass() {} // Correctly highlights "myclass" and not "::myclass()"
myclass::~myclass() {} // Correctly highlights "myclass" and not "::~myclass()"
bool myclass::myfunc() {} // Does not highlight "myclass"
If I change out \w
with .
like this:
call matchadd('UserTypes', ignoreHiInComments.'\zs.\+\ze::.\+')
Then I get this highlighting:
myclass::myclass() {} // Correctly highlights "myclass" and not "::myclass()"
myclass::~myclass() {} // Correctly highlights "myclass" and not "::~myclass()"
bool myclass::myfunc() {} // Highlights all of "bool myclass", this is incorrect
If I try to match whole words like this:
call matchadd('UserTypes', ignoreHiInComments.'\<\zs.\+\ze::.\+\>')
Then I get this result:
myclass::myclass() {} // Correctly highlights "myclass" and not "::myclass()"
myclass::~myclass() {} // Correctly highlights "myclass" and not "::~myclass()"
bool myclass::myfunc() {} // Does not highlight "myclass"
I have also tried adding various white space matching elements to my regex, but it hasn't worked.
Can anyone help me understand what I am doing wrong?