匹配双下划线的正则表达式?
我正在尝试扩展 python.lang
文件,以便能够高亮显示像 __init__
这样的特殊方法。我一直在想办法写一个正则表达式,能够匹配所有的 __privateMethods()
。
python.lang
是一个 XML 文件,里面包含了所有 Python 文件的高亮规则。例如:
<context id="special-variables" style-ref="special-variable">
<prefix>(?<![\w\.])</prefix>
<keyword>self</keyword>
<keyword>__name__</keyword>
<keyword>__debug__</keyword>
</context>
我该如何扩展这个文件,以便它能匹配双下划线呢?
[解决方案]: 我在我的 python.lang
文件中添加的内容(如果有人感兴趣的话):
首先,你需要在样式定义的顶部附近添加这一行。
<style id="private-methods" _name="Private Methods" map-to="def:special-constant"/>
然后你要添加正则表达式,Carles 在他的回答中提供的:
<context id="private-methods" style-ref="private-methods">
<match>(__[a-zA-Z_]*(__)?)</match>
</context>
完成后,它的样子是这样的!
2 个回答
1
将你之前的情况与下面的内容进行匹配(rubular 示例):
(^__[a-z]*__$)
5
应该是这样的:
(__[a-zA-Z0-9_]*(__)?)
为了匹配以下所有内容:
__hello()
__init__()
__this_is_a_function()
__this_is_also_a_function__()
__a_URL2_function__()