我目前正在研究一個簡單的模板引擎。在模板中,可以使用 if 陳述句。一個 if 塊看起來像這樣
{% name IF: a EQUALS b %}
content
{% name ENDIF %}
我想通過正則運算式識別這些塊。問題是我需要一個包含兩個未知但相等部分的正則運算式模式。這是與所有塊匹配的模式:
/{% (.*) IF: (.*) %}([\s\S]*){% (.*) ENDIF %}/gm
為了闡明哪個 ENDIF 標記屬于哪個 IF,第一個和最后一個捕獲組需要相同。有沒有辦法做到這一點?
uj5u.com熱心網友回復:
您可以使用此正則運算式:
{%\s (\S )\s IF:. ?%}(?s)(. ?){%\s \1\s ENDIF\s %}
正則運算式演示
正則運算式詳情:
{%: 比賽{%\s: 匹配 1 個空格(\S ):\s: 匹配 1 個空格IF:: 比賽IF:. ?: 匹配任意字符的 1 (非貪婪)%}: 比賽%}(?s): 啟用 DOTALL 模式,使點匹配換行符(. ?): 第一個捕獲組匹配 1 的任何字符{%: 比賽{%\s: 匹配 1 個空格\1:匹配與我們在捕獲組 #1 中捕獲的值相同的值\s: 匹配 1 個空格ENDIF: 比賽ENDIF\s: 匹配 1 個空格%}: 比賽結束%}
uj5u.com熱心網友回復:
您可以匹配以下正則運算式(設定了 general g、 multilinem和 case-indifferenti標志)。
{% ([a-z] ) IF:.*? %}\r?\n(?:^.*\r?\n)*?{% \1 ENDIF %}$
演示
運算式分解如下(或者,將游標懸停在 regex101.com 鏈接上的運算式的每個元素上以獲得對其功能的解釋)。
{% # match `{%` followed by one or more spaces
([a-z] ) # match one or more letters followed by one or more spaces
IF:.*? %}\r?\n # match 'IF: followed by zero or more characters, matched
# lazily, followed by one or more spaces followed by '%}'
# followed by a line terminator (`\r?` to satisfy Windows)
(?: # begin non-capture group
^ # match beginning of line
.*\r?\n # match one or more characters other than newlines followed
# by a line terminator
)*? # end non-capture group and execute it zero or more times, lazily
{% # match `{%` followed by one or more spaces
\1 # match the content of capture group 1 followed by one or
# more spaces
ENDIF # match `ENDIF` followed by one or more spaces
%}$ # match `{%` at the end of a line
該鏈接表明,如果文本塊是:
{% Saffi IF: my { % dog } has fleas %}
content
{% Saffi ENDIF %}
它也將匹配。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/354308.html
上一篇:一個單詞的Python正則運算式
下一篇:我需要在某種模式之間保留任何東西
