Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
WYS Avatar asked Nov 03 '12 15:11

WYS


2 Answers

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>
like image 57
Eric Avatar answered Oct 26 '22 06:10

Eric


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)
like image 45
enrico.bacis Avatar answered Oct 26 '22 04:10

enrico.bacis