Python regular schedule to match entire string

I am new to scripting and have read about how to use regular expressions.

I want to get the full string matching the pattern.

my ouptut:

64 bytes from 33.33.33.33: icmp_seq=9 ttl=254 time=1.011 ms

--- 33.33.33.33 ping statistics ---
10 packets transmitted, 10 packets received, 0.00% packet loss

I tried writing a regular expression comparing packet loss and tried to get the full string, but could not get it to work.

can someone help me ..

cmd = re.search('(\d*)% packet loss', ping_result[int(i)], re.M|re.I)
print cmd.group()

But this conclusion is only printed

00% packet loss
00% packet loss
+4
source share
3 answers

First, you want to use raw strings when providing a regex string, this is done by prefixing the string with r, otherwise the escape sequences will be consumed.

\d , , . , r'(\d+\.\d+)'

, -, , .*, . :

r'.*(\d+\.\d+)% packet loss'

, ^ (start) $ (end)

r'^.*(\d+\.\d+)% packet loss$'
+3

Try

cmd = re.search('^.*\d*% packet loss.*$', ping_result[int(i)], re.M|re.I)
print cmd.group()

'^' '$' , . , .

+4

, , " ".

for line in lines:
    if line.find('packet loss') != -1:
        print line
+1

Source: https://habr.com/ru/post/1568381/


All Articles