简体   繁体   中英

Matching n number of lines in text to a regex any order using python

I have to match the configuration of a filewall from a input file. In input file I'll specify a regex. This regex should match the output of the command from firewall.

Suppose the firewall output is as below

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

I want to match 9 lines as below regex.

(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}

But the rule will not match because of the 7th line when I use this regex with re.match or re.search.

Is there any way to check this regex in any order. I mean it should match 9 lines, even if some unwanted lines are there in between.

Update, This is how i use it

 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. 

ok. To make it more clear, my pattern works for the following 14 lines.

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

But when we add another line

ssh 172.31.1.30 255.255.255.255 junk_string

then the regex fails for

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

my requirement is the regex should work as long as the 14 lines are there. Even if the extra line comes before/after/in between.

How can we achieve this?

If you want to match a dot literally you should escape it \\. or else it would mean to match any character. There is also a leading s missing, now it says sh .

If you use an alternation and don't want to capture the values you could use a non capturing group (?:

To to match all the lines except ssh 192.115.24.64 255.255.255.224 junk_string you might use findall .

Without capturing groups you could match your lines with:

ssh 192\\.217\\.(?:254\\.(?:2[015]|38|42)|255\\.78|24[08]\\.0|236\\.0) 255\\.255\\.(?:255\\.255|252\\.0) [^ ]*\\r?\\n|\\r

Demo Python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM