Search code examples
vscode-extensionsgrammartextmate

Visual Studio Code TextMate match pattern with maximum possible length first


I am writing a TextMate grammar for a syntax highlighting extension in VS Code, and I discovered that if I define match rules for constants, it matches them multiple times in a row. More specifically, I want to define various match rules that contain sequences of letters, but I want each of them to consider the longest possible sequence of consecutive letters before looking for a match. Is there any way to achieve this behavior?

Currently, if I have a constant defined as simply test, it matches the string testtest as two copies of the constant, but I would prefer it to consider the entire string and therefore not match the test constant at all. If it matters, the fallback in this case would be to match it as a variable. Basically, if the maximum consecutive string of letters is a constant, it should be matched as a constant, otherwise it should be matched as a variable, regardless of whether or not that variable name contains a substring that matches a constant.


Solution

  • you are looking for the word boundary anchor \\b.
    it will only match if there is only one word character next to it \\btest\\b.
    https://www.regular-expressions.info/wordboundaries.html