Search code examples
regexsublimetext2tmlanguage

Sublime text regular expressions tmLanguage


I am trying to modify a the xml file for a tmLanguage and i want to add a keyword to match word=. I can use obviously use regex to find word but when i add the = sign it can't find word=. I tried to escape the character but no luck. Any other ideas?

    <dict>
        <key>match</key>
        <string>(?:\b(word=)\b)</string>
        <key>name</key>
        <string>keyword.other.ixml</string>
    </dict>

Solution

  • You have word boundaries at both ends of word= and that means word= should be preceded by a non-word character (as the first \b is before a word character w) and followed by a word character (as the second \b is after a non-word character). It matches ,word=n, for example.

    More details about a word boundary:

    There are three different positions that qualify as word boundaries:

    • Before the first character in the string, if the first character is a word character.
    • After the last character in the string, if the last character is a word character.
    • Between two characters in the string, where one is a word character and the other is not a word character.

    If you plan to match word= in all cases where it starts with w, just use the first \b and remove the last one.

    So, replace <string>(?:\b(word=)\b)</string> with:

    <string>\bword=</string>
    

    See this regex demo

    I also removed unnecessary groupings (...).

    As an alternative, if you just do not want non-word characters appear before and after the `word=, use lookarounds:

    <string>(?<!\w)word=(?!\w)</string>