467,230 Members | 1,366 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,230 developers. It's quick & easy.

Python regular expression to pick values in a string

Hi
I have the output below stored in a variable called outping. I am trying to pick the values in bold into two variables using python regular expressions. i will appreciate your help. Thanks

Type escape sequence to abort.
Sending 100, 100-byte ICMP Echos to 192.168.1.8, timeout is 2 seconds:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Success rate is 100 percent (100/100), round-trip min/avg/max = 1/3/12 ms
switch1B#

Here is the code i am trying to use
Expand|Select|Wrap|Line Numbers
  1. patt = re.compile(r"(Success rate)", re.MULTILINE)     
  2. m = re.search(patt, outping)
  3. print m.group(1)
  4.  
Jul 3 '14 #1
  • viewed: 1318
Share:
1 Reply
Expert 512MB
I would do it like this since it is understandable and can be easily debugged and/or broken down.
Expand|Select|Wrap|Line Numbers
  1. test_string="Success rate is 100 percent (100/100), round-trip min/avg/max = 1/3/12 ms"
  2. if test_string.startswith("Success"):
  3.     test_list=test_string.split()
  4.     percent = ""
  5.     for ctr, word in enumerate(test_list):
  6.         if word == "percent":
  7.             percent = test_list[ctr-1]  ## previous word
  8.  
  9. print percent, test_list[-2].split("/")[-1] 
Jul 4 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by | last post: by
5 posts views Thread by ST Wong | last post: by
7 posts views Thread by Allerdyce.John@gmail.com | last post: by
2 posts views Thread by Ross Ylitalo | last post: by
4 posts views Thread by unexpected | last post: by
6 posts views Thread by Teja | last post: by
1 post views Thread by Wehrdamned | last post: by
5 posts views Thread by antar2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.