5

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

Community
  • 1
  • 1
Kredns
  • 36,461
  • 52
  • 152
  • 203

2 Answers2

5

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__()
Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60
  • Thanks I figured out how to add it. – Kredns Feb 04 '11 at 21:05
  • @Carles: I updated my answer to show how it can be added. Thanks for all your help. – Kredns Feb 04 '11 at 21:13
  • @Lucas nice... but you ended up using Mikhail's regex ;) – Carles Barrobés Feb 04 '11 at 21:39
  • @Carles: Whoops! I copied the wrong section of the file. I actually did end up using yours. ;) – Kredns Feb 04 '11 at 21:53
  • @Carles: I just noticed a bug. Your solution doesn't match words with uppercase letters. I've updated the regex in my question to fix that. – Kredns Feb 04 '11 at 22:51
  • @Lucas hm yes it did match uppercase. Anyway I just updated it to also accept digits after looking at http://docs.python.org/reference/lexical_analysis.html#identifiers – Carles Barrobés Feb 05 '11 at 20:33
  • at this point, it's simpler to just go with `(__[a-zA-Z0-9_]*)` if your final `__` is optional anyway. they match the same things and the main difference is whether you need a capture on `__`. – muhmuhten Jul 28 '14 at 18:05
1

Match your previous case piped with the following (rubular example):

(^__[a-z]*__$)
Mikhail
  • 8,692
  • 8
  • 56
  • 82
  • @Mikhail I think it should be (^__[a-z]+__$) to prevent matching ____. – OnesimusUnbound Feb 04 '11 at 20:30
  • @OnesimusUnbound: No I actually want it to match the second set of underscores as well. – Kredns Feb 04 '11 at 20:32
  • @Lucas McCoy you mean matching four underscores? – OnesimusUnbound Feb 04 '11 at 20:34
  • @OnesimusUnbound: I'm going to match `__methods()` and `__methods__()` so his regex is ok. I'm going to modify it a little. – Kredns Feb 04 '11 at 20:37
  • @OnesimusUnbound: That looks great, but when I add `()` it doesn't match. I don't want the parenthesis to be highlighted but it needs to match everything before them. Also you should post that as an answer. – Kredns Feb 04 '11 at 20:57
  • @Lucas McCoy changed to \w and added \(\) at the end(http://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes) http://regexr.com?2t1k7 – OnesimusUnbound Feb 04 '11 at 21:06