【发布时间】:2014-11-11 06:54:46
【问题描述】:
示例输入
1 0.000000 10.19.20.105 -> 74.125.236.200 ICMP 74 Echo (ping) request id=0x000a, seq=51187/62407, ttl=128
6 0.097977 74.125.236.194 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62844/31989, ttl=57 (request in 2)
7 0.131456 74.125.236.198 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62845/32245, ttl=57 (request in 3)
8 0.143539 74.125.236.196 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62847/32757, ttl=57 (request in 5)
9 0.160567 74.125.236.192 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62846/32501, ttl=57 (request in 4)
10 0.177972 10.19.20.172 -> 10.19.20.255 NBNS 92 Name query NB INDERPAL-PC<1c>
11 0.270418 10.19.20.105 -> 74.125.236.194 ICMP 74 Echo (ping) request id=0x000b, seq=62848/33013, ttl=128
12 0.318404 10.19.20.105 -> 74.125.236.194 ICMP 74 Echo (ping) request id=0x000b, seq=62849/33269, ttl=128
13 0.330236 74.125.236.194 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62848/33013, ttl=57 (request in 11)
14 0.376039 74.125.236.194 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62849/33269, ttl=57 (request in 12)
17 0.397384 10.19.20.105 -> 74.125.236.195 ICMP 74 Echo (ping) request id=0x000b, seq=62852/34037, ttl=128
18 0.438108 74.125.236.200 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62850/33525, ttl=57 (request in 15)
19 0.444489 10.19.20.105 -> 74.125.236.196 ICMP 74 Echo (ping) request id=0x000b, seq=62853/34293, ttl=128
21 0.463515 74.125.236.195 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62852/34037, ttl=57 (request in 17)
22 0.475425 10.19.20.105 -> 74.125.236.197 ICMP 74 Echo (ping) request id=0x000b, seq=62854/34549, ttl=128
25 0.522472 74.125.236.197 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62854/34549, ttl=57 (request in 22)
26 0.535794 Giga-Byt_5d:06:ac -> Broadcast ARP 60 Who has 10.19.20.74? Tell 10.19.20.94
27 0.537735 Giga-Byt_a0:ad:23 -> Broadcast ARP 60 Who has 10.19.20.94? Tell 10.19.20.74
28 0.550321 10.19.20.105 -> 74.125.200.95 TCP 55 58240→80 [ACK] Seq=1 Ack=1 Win=16402 Len=1
29 0.574957 JetwayIn_a0:b1:a2 -> Broadcast ARP 60 Who has 10.19.20.180? Tell 10.19.20.172
30 0.584448 74.125.236.195 -> 10.19.20.105 ICMP 74 Echo (ping) reply id=0x000b, seq=62855/34805, ttl=57 (request in 24)
public class DataGridClass
{
public int SerialNumber { get; set; }
public string Time { get; set; }
public string DestinationIP { get; set; }
public string SourceIP { get; set; }
public string Protocol { get; set; }
public int Length { get; set; }
public string Info { get; set; }
}
期望的输出
SerialNumber = 1
Time = "0.000000"
DestinationIP = "10.19.20.105"
SourceIP = "74.125.236.200"
Protocol = "ICMP"
Length = 74
Info = "Echo (ping) request id=0x000a, seq=51187/62407, ttl=128"
我无法用空格分割,因为字符串不一致,其次空格的数量可能会增加或减少
提前致谢
更新:
26、27和别人不一样
【问题讨论】:
-
你只给了我们一个例子——我们要如何解决它可能不一致的地方?限制执行的拆分次数很容易,但我们不知道您所说的不一致是什么意思...
-
使用
test.Split(new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries),将前7个子字符串(跳过“->”)分配到类中,将其余部分放入Info。 -
除非您正在练习拆分/正则表达式,否则已经有 Ping 类。
-
/(\d+)\s+([\d\.]+)\s+([\d.]+)[\s->]+([\d\.]+) \s+([a-zA-Z]+)\s+([\d]+)\s+(.+) - 试试这个。
-
@AlexeiLevenkov 是的,我知道 Ping 类,但是如何在不丢失信息的情况下将这些数据解析到该类?