RegExp for PING success/failure

  • Thread starter Thread starter Precious
  • Start date Start date
P

Precious

Using RegExp, I want to capture PING command success/failure and do the
further processing. Can anybody help?

TIA

J Justin
 
Hi Precious,

We can't do a RegEx without an example of the strings to match against.
Have you some examples of PING result strings, and what you want out of them?

Regards,
Fergus
 
Sorry for posting incomplete question.

My ping command will be something like this.
ping xx.xx.xx.xx
The last line of ping output comes as
Packets: Sent = 4, Received = 0, Lost = 0 (100% lost)
I want to capture the number of packets sent, received and lost.

As such, the syntax of RegEx is complex and also not user friendly... I
always want somebody's help whenever I get trouble in RegEx.

Thanks in advance

J Justin
 
Precious
As such, the syntax of RegEx is complex and also not user friendly... I
always want somebody's help whenever I get trouble in RegEx.

I never use the RegEx, personaly I find it complex and afterwards unreadable
special for others.
I would do in a way it like this.
\\\\
Dim Pingstring As String = "Packets: Sent = 4, Received = 0, Lost = 13
(100% lost)"
Dim PingStart As Integer
Dim PingEnd As Integer
PingStart = Pingstring.IndexOf("Sent =") + 6
PingEnd = Pingstring.IndexOf(",")
MessageBox.Show(Pingstring.Substring(PingStart, PingEnd -
PingStart))
PingStart = Pingstring.IndexOf("Received =", PingEnd) + 10
PingEnd = Pingstring.IndexOf(",", PingStart)
MessageBox.Show(Pingstring.Substring(PingStart, PingEnd -
PingStart))
PingStart = Pingstring.IndexOf("Lost =", PingEnd) + 6
PingEnd = Pingstring.IndexOf("(", PingStart) - 1
MessageBox.Show(Pingstring.Substring(PingStart, PingEnd -
PingStart))
////
But I do not know if that is a sollution for your problem?
Cor
 
Hi Justin,

|| the syntax of RegEx is complex and also not user friendly

Is that true or is that true!

Here's a non-RegEx way to do it:

Dim sPingResult As String = "Packets: Sent = 4, Received = 5, Lost = 6
(100% lost)"
Dim alResults() As String = sPingResult.Split (New Char() {"="c, ","c,
"("c, "%"c})

Dim iSent As Integer = Integer.Parse (alResults(1))
Dim iReceived As Integer = Integer.Parse (alResults(3))
Dim iLost As Integer = Integer.Parse (alResults(5))
Dim iPercent As Integer = Integer.Parse (alResults(6))

MsgBox ("Sent = " & iSent & ", Received = " & iReceived _
& ", Lost = " & iLost & ", Percent = " & iPercent & "%")

If you really want an RE it can be arranged. :-)

Regards,
Fergus
 
Hi,

A regular expression to match your ping output would be

Packets:\s+Sent = (?<sent>\d+), Received = (?<rcvd>\d+), Lost = (?<lost>\d+)

This regex will store the results in these groups: sent, rcvd and lost

You can use my regex design tool to help test regular expressions.
 
Back
Top