簡體   English   中英

正則表達式匹配雙下划線?

[英]Regex to match double underscores?

我正在嘗試擴展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>

如何擴展它以匹配雙下划線?


[解決方案]:我添加到我的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>

這就是你完成后的樣子!

在此輸入圖像描述

它應該是:

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

為了匹配以下所有內容:

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

使用以下( rubular示例 )匹配您之前的案例管道:

(^__[a-z]*__$)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM