473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python regular expression to pick values in a string

20 New Member
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 1512
dwblas
626 Recognized Expert Contributor
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
2321
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 <HTML> tag with nothing?
1
1283
by: ÒÊÃÉɽÈË | last post by:
thanks
5
7018
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
3158
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 totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1 and I would like to convert it to: rect x="11" y="0" width="760" height="14440"
2
1688
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 possible or not? Even better, can anyone show the regular expression I am looking for? Thank you, Ross Ylitalo
4
1674
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. ('MULTX-' is in this list, and hence a keyword). My regular expression works for a variety of different keywords except
6
1325
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 character 3) it can contain "" or "" at the end. but this is optional can any one please help me out.... pls....
1
2824
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 ? File "/usr/lib/python2.4/sre.py", line 180, in compile return _compile(pattern, flags)
5
2215
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 are in list1, into for example 'u'.
4
1770
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 USAGE in the second column based on the supplied queue number in the first column Thanks in advance. import re content = """NUMBER of Queues = 5
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.