473,230 Members | 1,330 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,230 software developers and data experts.

String - How to

Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
Sep 14 '07 #1
6 1544
ilikepython
844 Expert 512MB
Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
Maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. patt = re.compile(r"\([0-9\.]+% loss\)")
  3. l = re.findall(patt, line)
  4. print l[0].split("%")[0][1:]
  5.  
Sep 14 '07 #2
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> import re
  3. >>> m = re.search(r"\(([0-9\.]+)% loss\)", line)
  4. >>> m.group(1)
  5. '100'
  6. >>> 
Sep 14 '07 #3
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> import re
  3. >>> m = re.search(r"\(([0-9\.]+)% loss\)", line)
  4. >>> m.group(1)
  5. '100'
  6. >>> 
Much easier, thanks barton.
Sep 14 '07 #4
Thanks for the replies, I appreciate a lot.
I will try this during the weekend. I will get back to you on monday
if I need a little help but I think that this will be good enough : )

tks, tks, tks.

Charlie of Bolton!
Sep 14 '07 #5
bartonc
6,596 Expert 4TB
Much easier, thanks barton.
That's actually your regex with () added to group the sought-after part.
I'm still not very proficient at writing the regex part, so thank you for that.
Sep 14 '07 #6
ghostdog74
511 Expert 256MB
Hi,

Here's a result from a ping:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Pinging 10.2.3.6 with 32 bytes of data:
  3.  
  4.  
  5. Request timed out.
  6.  
  7. Ping statistics for 10.2.3.6:
  8.  
  9.     Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),
  10.  
  11.  
I`m trying to filter only the number before : loss in this case: 100%,
but I`m doing something wrong obviously because I receive:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <_sre.SRE_Match object at 0x00C461E0>
  3.  
  4.  

Here is my code, what do you suggest ?

Expand|Select|Wrap|Line Numbers
  1.  
  2. def run(self):
  3.       pingaling = os.popen("ping "+self.ip"r")
  4.  
  5. # This code is what each parallel thread does
  6. # the 'run' method is triggered when 'start' is called
  7.       while 1:
  8.          line = pingaling.read()
  9.          line = pingaling.read() 
  10.          mo = re.search("loss", line) 
  11.          if not line: break
  12.          print
  13.          print mo 
  14.  
  15.  
how about some basic string manipulation without regular expressions?
Expand|Select|Wrap|Line Numbers
  1. >>> line = "Packets: Sent = 1, Received = 1, Lost = 1 (100% loss),"
  2. >>> startopenbracket = line.find("(")
  3. >>> startloss = line.find("loss")
  4. >>> print line[startopenbracket+1:startloss]
  5. 100%
  6.  
Sep 15 '07 #7

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

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.