Search code examples
pythonxmlregexgedit

Regex to match double underscores?


I'm trying to extend the python.lang file so that it will make methods like __init__ highlighted. I've been trying to come up with a Regex that will match all __privateMethods().

The python.lang is a XML file containing all of the highlighting rules for python files. Ex:

<context id="special-variables" style-ref="special-variable">
   <prefix>(?&lt;![\w\.])</prefix>
   <keyword>self</keyword>
   <keyword>__name__</keyword>
   <keyword>__debug__</keyword>
</context>

How can I extend this so that it matches double underscores?


[SOLUTION]: What I added to my python.lang file (if anyone's interested):

First off you need to add this line near the top where the styles are defined.

<style id="private-methods" _name="Private Methods" map-to="def:special-constant"/>

Then you'll add the Regex that Carles provided in his answer:

<context id="private-methods" style-ref="private-methods">
    <match>(__[a-zA-Z_]*(__)?)</match>
</context>

And here is what it looks like when your done!

enter image description here


Solution

  • It should rather be:

    (__[a-zA-Z0-9_]*(__)?)
    

    In order to match all of the following:

    __hello()
    __init__()
    __this_is_a_function()
    __this_is_also_a_function__()
    __a_URL2_function__()