Search code examples
regexvisual-studio-codevscode-extensionstextmate

Textmate Grammar - Highlight 'STR$' call


I had this regex for capturing some standard lib calls in a TextMate (vs code) grammar:

(?i)\b(sin|cos|tan)\b

Now, in a call like str$(5), I'd like to capture str$. You can't do:

(?i)\b(sin|cos|tan|str\$)\b

Because of the ending word boundary (I think?)

I tried

(?i)\b(sin|cos|tan|str\$)( |\()

But that causes:

wrongBracketHighlighting

Can you capture a word with a special character on the end & word boundaries either side (without "capturing" the actual chars on the boundaries)?


Solution

  • You may use

    (?i)\b(sin|cos|tan|str\$)(?!\w)
                             ^^^^^^
    

    Or

    (?i)(?<!\w)(sin|cos|tan|str\$)(?!\w)
        ^^^^^^^                   ^^^^^
    

    The (?!\w) is a negative lookahead that will fail the match if, immediately to the right of the current location, there is a letter, digit or an underscore.

    The (?<!\w) is a negative lookbehind that will fail the match if, immediately to the left of the current location, there is a letter, digit or an underscore.