import re
data = []
tcp_dump = "17:18:38.877517 IP 192.168.0.15.43471 > 23.195.155.202.443: Flags [.], ack 1623866279, win 245, options [nop,nop,TS val 43001536 ecr 287517202], length 0"
regex = r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(^length (\d+))'
data_ready = re.findall(regex, tcp_dump)
print(data_ready)
data.append(data_ready)
print(data)
該代碼當前需要獲取2個IPv4地址和一個數據包的長度並將它們轉換爲2-d列表。到目前爲止,我的正則表達式的前半部分只是使用IPv4地址。我的問題歸結爲抓住長度。我得到的輸出:從SUBSTRING獲取INT的正則表達式
[('192.168.0.15', '', ''), ('23.195.155.202', '', '')]
,而不是期望的輸出:
['192.168.0.15', '23.195.155.202', '0']
任何方式來解決這個正則表達式?
編輯
所以事實證明,正則表達式分隔作品(只是上半年或只是下半年),我似乎無法讓他們的工作結合起來。
但這'192.168.0.15.43471'是無效的IP地址 – RomanPerekhrest
是,192.168.0.15是IPv4,則43471是一個mac地址 –