Search code examples
regexvimsyntaxmatchlookbehind

Vim syntax region - lookbehind confusion


Define the following in .vimrc or execute within vim command line:

syn match ndbMethods "[^. \t\n\r]\@<=[_a-z][_a-zA-Z0-9]*(\@="
hi ndbMethods guibg=#222222

View results with a C-style method call in the active buffer:

foo();

You will see the initial character of the method name is not matched.

The intention is for the lookbehind pattern to force a beginning of line, literal . or whitespace to precede any matched method's first character.

Oddly enough, making this a negative lookahead (\@<!) seems to work!

Would someone be kind enough to explain why this lookbehind is incorrect?


Solution

  • Updated: At f, looking behind, you probably want to check for [. \t\n\r], not [^. \t\n\r]. Because currently, you're saying "something that doesn't follow one of these characters", so only upon reaching the o is that condition met, since f is indeed not one of those characters. So you have to either un-negate the character class, or as you discovered, negate the lookbehind.

    I think you're getting your terms confused, too.

    \@<=    positive lookbehind
    \@<!    negative lookbehind
    \@=     positive lookahead
    \@!     negative lookahead