正则表达式匹配双下划线?

Kre*_*dns 5 python regex xml gedit

我正在尝试扩展该python.lang文件,以便它可以使方法突出__init__显示。我一直在尝试想出一个能够匹配所有__privateMethods().

python.lang是一个 XML 文件,包含 python 文件的所有突出显示规则。前任:

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

我如何扩展它以使其匹配双下划线?


[解决方案]:我添加到我的python.lang文件中的内容(如果有人感兴趣):

首先,您需要在定义样式的顶部附近添加此行。

<style id="private-methods" _name="Private Methods" map-to="def:special-constant"/>
Run Code Online (Sandbox Code Playgroud)

然后您将添加卡尔斯在他的答案中提供的正则表达式:

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

这是完成后的样子!

在此输入图像描述

Car*_*bés 5

它应该是:

(__[a-zA-Z0-9_]*(__)?)
Run Code Online (Sandbox Code Playgroud)

为了匹配以下所有内容:

__hello()
__init__()
__this_is_a_function()
__this_is_also_a_function__()
__a_URL2_function__()
Run Code Online (Sandbox Code Playgroud)