[英]Regex (PHP flavour) to match over multiplie lines OR not multiple lines
我正在研究一種正則表達式,該正則表達式將同時符合以下兩個語法...
1。
aaa accounting system default
action-type start-stop
group tacacs+
2。
aaa accounting system default start-stop group tacacs+
到目前為止我最好的是...
^aaa accounting system default (\\n action-type |)start-stop(\\n |) group tacacs\\+
上面的正則表達式將匹配語法編號2而不匹配1? 拉我的頭發! (我知道這可能很簡單,但我是Regex新手)有什么想法嗎? 在語法段號1的第2行和第3行的開頭有空格,但是沒有顯示出來以使您真正了解語法的呈現方式,請查看下面的Regex101鏈接。 謝謝!
在Regex101中...
它不起作用,因為可選組中有多余的空格:
^aaa accounting system default(\n action-type|) start-stop(\n|) group tacacs\+
您可以使用非捕獲組(?:...)
和可選的量詞?
更好地編寫它?
:
^aaa accounting system default(?:\n action-type)? start-stop\n? group tacacs\+
(這樣可以避免無用的捕獲)
要跨多行匹配,您將需要DOTALL
標志:
/(?s)\baaa accounting system default.*?group tacacs\+/
要不然:
/\baaa accounting system default.*?group tacacs\+/s
您可以將\\s
中的常規空格替換為與任何空格匹配的\\s
:
'~^aaa\s+accounting\s+system\s+default(?:\s+action-type)?\s+start-stop\s+group\s+tacacs\+~m'
另外,我進行了一些其他優化,以便可以匹配兩種類型的字符串:
^
-匹配行首(由於/m
) aaa\\s+accounting\\s+system\\s+default
aaa accounting system default
匹配序列aaa accounting system default
,其中\\s+
匹配一個或多個空格 (?:\\s+action-type)?
-可選的action-type
(在action-type
之前有一個或多個空格) \\s+start-stop\\s+group\\s+tacacs\\+
-匹配單詞之間有1個或多個空格的start-stop group tacacs+
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.