473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python regular expression to pick values in a string

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
1 1509
dwblas
626 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

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

Similar topics

3
by: | last post by:
I am trying to edit a bunch of files that are similar. I want to remove all the ASP code that appears before the <HTML> tag. Can some one help me with a regex that can replace everything before the...
1
by: ÒÊÃÉɽÈË | last post by:
thanks
5
by: ST Wong | last post by:
Hi all, As metacharaters ^ and $ are not used in regular expression in XML, I'm looking for how to specify empty string. Would anyone please help? Thanks a lot. Regards, ST Wong
7
by: Allerdyce.John | last post by:
Hi, I am new to python. I would like to know how to use python regular expression to substitute string value? I have an input string like this: x:11 y:0 w:760 h:19 area:14440 areaPerCent:0...
2
by: Ross Ylitalo | last post by:
I have been trying to put together a regular expression that will trap any string with the exact string "&#" (without the quotes) and have been having no luck. Can anyone tell me if this is...
4
by: unexpected | last post by:
I'm trying to do a whole word pattern match for the term 'MULTX-' Currently, my regular expression syntax is: re.search(('^')+(keyword+'\\b') where keyword comes from a list of terms....
6
by: Teja | last post by:
HI all, I need to write a regular experssion for a string which satisfies the following a criteria : 1) it should start with an alphabet 2) it can contain alphabets/digits/_ from second...
1
by: Wehrdamned | last post by:
Hi, As I understand it, python uses a pcre engine to work with regular expression. My question is, then, why expressions like : Traceback (most recent call last): File "<stdin>", line 1, in...
5
by: antar2 | last post by:
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = list2 = Suppose that I want to substitute the vowels from list2 that...
4
by: kaf3773 | last post by:
Hi I am trying to write a python script that executes a command to screen scrub results below I will appreciate it very much if you can help me with a python script that can pick the percentage...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.