[英]Matching n number of lines in text to a regex any order using python
我必須從輸入文件中匹配文件牆的配置。 在輸入文件中,我將指定一個正則表達式。 此正則表達式應與來自防火牆的命令輸出匹配。
假設防火牆輸出如下
ssh 192.217.254.20 255.255.255.255 junk_string
ssh 192.217.248.0 255.255.252.0 junk_string
ssh 192.217.254.21 255.255.255.255 junk_string
ssh 192.217.254.25 255.255.255.255 junk_string
ssh 192.217.254.38 255.255.255.255 junk_string
ssh 192.217.254.42 255.255.255.255 junk_string
ssh 192.115.24.64 255.255.255.224 junk_string
ssh 192.217.240.0 255.255.252.0 junk_string
ssh 192.217.236.0 255.255.252.0 junk_string
ssh 192.217.255.78 255.255.255.255 junk_string
我想匹配9行如下正則表達式。
(sh 192.217.((254.(20|21|25|38|42)|255.78) 255.255.255.255|(240|248|236).0 255.255.252.0) [^ ]*\r\n?){9}
但是,當我將此正則表達式與re.match或re.search一起使用時,由於第7行,該規則將不匹配。
有沒有辦法以任何順序檢查此正則表達式。 我的意思是,即使它們之間有一些不需要的行,也應該匹配9行。
更新,這就是我的使用方式
if re.match(result_expected[command],actual_result,re.M|re.I):
if verbose == "True":
print("Command output and expected output Matched")
result_expected - is our regex I have given above.
actual_result - is the 10 lines which is output of the command executed on the firewall.
好。 更清楚地說,我的模式適用於以下14行。
pattern='(ssh 192.(168.((254.(2|6|20|21|25|38|42)|255.78|255.91|255.92) 255.255.255.255|(240|248|236).0 255.255.252.0)|115.24.64 255.255.255.224) [^ ]*\r?\n?){14}'
ssh 192.168.240.0 255.255.252.0 junk_string
ssh 192.168.248.0 255.255.252.0 junk_string
ssh 192.168.236.0 255.255.252.0 junk_string
ssh 192.168.254.42 255.255.255.255 junk_string
ssh 192.168.254.25 255.255.255.255 junk_string
ssh 192.168.254.21 255.255.255.255 junk_string
ssh 192.168.255.78 255.255.255.255 junk_string
ssh 192.168.254.20 255.255.255.255 junk_string
ssh 192.168.254.38 255.255.255.255 junk_string
ssh 192.115.24.64 255.255.255.224 junk_string
ssh 192.168.254.2 255.255.255.255 junk_string
ssh 192.168.254.6 255.255.255.255 junk_string
ssh 192.168.255.91 255.255.255.255 junk_string
ssh 192.168.255.92 255.255.255.255 junk_string
但是當我們添加另一行時
ssh 172.31.1.30 255.255.255.255 junk_string
然后正則表達式失敗
ssh 192.168.240.0 255.255.252.0 junk_string
ssh 192.168.248.0 255.255.252.0 junk_string
ssh 192.168.236.0 255.255.252.0 junk_string
ssh 192.168.254.42 255.255.255.255 junk_string
ssh 192.168.254.25 255.255.255.255 junk_string
ssh 192.168.254.21 255.255.255.255 junk_string
ssh 192.168.255.78 255.255.255.255 junk_string
ssh 192.168.254.20 255.255.255.255 junk_string
ssh 192.168.254.38 255.255.255.255 junk_string
ssh 172.31.1.30 255.255.255.255 junk_string
ssh 192.115.24.64 255.255.255.224 junk_string
ssh 192.168.254.2 255.255.255.255 junk_string
ssh 192.168.254.6 255.255.255.255 junk_string
ssh 192.168.255.91 255.255.255.255 junk_string
ssh 192.168.255.92 255.255.255.255 junk_string
我的要求是,只要有14行,正則表達式就可以正常工作。 即使多余的行位於之前/之后/之間。
我們怎樣才能做到這一點?
如果要從字面上匹配點,則應將其轉義\\.
否則就意味着要匹配任何字符。 現在還說sh
,這也是一個前導s
缺失。
如果您使用輪換方式並且不想捕獲值,則可以使用非捕獲組(?:
要匹配除ssh 192.115.24.64 255.255.255.224 junk_string
之外的所有行,可以使用findall 。
如果不捕獲組,則可以將行與以下項匹配:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.