regexsyntax-highlightingsublimetext3sublimetextoniguruma

Get the first word in a pattern (using oniguruma on SublimeText)


When trying to define a new language that extends HTML on SublimeText, i can't seem to find the right syntax for getting the first word on a pattern.

my goal is when this will appear:

[[something\something else else else]]

that the words "something" will have a different scope.

my pattern in the tmLanguage file is defined like so:

<key>begin</key>
<string>(\[\[)</string>
<key>end</key>
<string>(\]\])</string>
<key>name</key>
<string>text.html.foo.fam<string>

i tried adding the following pattern without any luck:

<key>patterns</key>
<array>
    <dict>
        <key>match</key>
        <string>(^(.*?([a-zA-Z\\]+.*?)){1})</string>
        <key>name</key>
        <string>entity.name.function</string>
    </dict>
</array>

Solution

  • First if I were you I wouldn't write a .tmlanguage right now as they are being removed from sublime. Instead a new format has been created by Jon. You'll find more information on the forum.

    Second the problem with your regex is that you try to match spaces with .*. But .* match anything. Use \s* instead.

    Something like ^\s*([A-Za-z\\]+)\b should do the trick. The \b means word boundary.

    Edit: I read your question to fast. I put ^ to match the beginning of the line but, in your case it won't work with [[ something else ]] because the line begin on [[ not a blank. So you may want to replace ^ with (?&lt;=\[) that will check if the previous matched character is a [.

    With both (?&lt;=\[|^)\s*([A-Za-z\\]+)\b I got the following highlighting:

    screenshot