WYS
WYS

Reputation: 1647

Sublime Text's syntax highlighting of regexes in python leaks into surrounding code

I have a problem with sublime text which should be generally all editors. When I have a regular expression like this.

listRegex = re.findall(r'[*][[][[].*', testString)

All the text after the regular expression will be incorrectly highlighted, because of the [[], specifically the [ without a close bracket. While the intention of this regular expression is correct, the editor doesn't know this.

This is just an annoyance I don't know how to deal with. Anyone know how to fix this?

Upvotes: 5

Views: 2221

Answers (2)

Eric
Eric

Reputation: 97571

While it doesn't really answer your question, you could just use a different regex:

listRegex = re.findall(r'\*\[\[.*', testString)

Or you can prevent any regex highligting:

listRegex = re.findall(R'[*][[][[].*', testString)

Proper solution

Add the following to .../Packages/Python/Regular Expressions (Python).tmLanguage on line 266 (first and third blocks are context):

<key>name</key>
<string>constant.other.character-class.set.regexp</string>
<key>patterns</key>
<array>
    <dict>
        <key>match</key>
        <string>\[</string>
    </dict>
    <dict>
        <key>include</key>
        <string>#character-class</string>
    </dict>

Upvotes: 6

enrico.bacis
enrico.bacis

Reputation: 31494

That's a known bug of Sublime Text's Python Syntax Highlighter that only affects raw strings.

By the way in a regex you can match a special character in two ways:

  1. Enclosing it in a square bracket: [[]

  2. Escaping it with a backslash: \[

The second one is preferred, so you can change your code to:

listRegex = re.findall(r'\*\[\[.*', testString)

Upvotes: 2

Related Questions