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

2

5

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?

Gosnell answered 3/11, 2012 at 15:54 Comment(2)
Just put the closing braces in a comment at the end of the line. The line itself may not look good but at least it doesn't taint the rest of the code highlighting.Uncommercial
You'll need to modify the Package/Python/Regular Expressions (Python).tmLanguage file to fix thisPudgy
P
6

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>
Pudgy answered 3/11, 2012 at 16:39 Comment(4)
By the way, thanks for the answer. And all other who tried to help :)Gosnell
What does the change of r to R do? I quite don't understand the sentence above.Strong
@Qwerty: Python doesn't care which you use. Sublime text will only syntax highlight regexes if the raw string uses a lowercase r. I don't know if this is by design or accidentalPudgy
@Pudgy So we are actually exploiting a bug, haha, clever! :)Strong
E
2

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)
Eventful answered 3/11, 2012 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.