By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Python regular expression to pick values in a string

P: 20
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
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 614
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.