2017-10-16 25 views
-1
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']

任何方式來解決這個正則表達式?

編輯

所以事實證明,正則表達式分隔作品(只是上半年或只是下半年),我似乎無法讓他們的工作結合起來。

+0

但這'192.168.0.15.43471'是無效的IP地址 – RomanPerekhrest

+0

是,192.168.0.15是IPv4,則43471是一個mac地址 –

回答

0

這應該做到這一點。你只需要做出一些你的括號非獲取的,並做一些數據清理

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+))' 

# make the returned tuples into two lists, one containing the IPs and the 
# other containing the lengths. Finally, filter out empty strings. 
data_ready,lengths = zip(*re.findall(regex, tcp_dump)) 
list_data = [ip for ip in list(data_ready) + list(lengths) if ip != ''] 
print(list_data) 
data.append(list_data) 

print(data) 

輸出:

['192.168.0.15', '23.195.155.202', '0'] 
0

我不會把它的IP地址匹配(如192.168.0.15.43471是無效的IP地址),但文本解析/處理。
re.search()功能優化的解決方案:

import re 

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" 
result = re.search(r'((?:\d{1,3}\.){3}\d{1,3})(?:\.\d+) > ((?:\d{1,3}\.){3}\d{1,3})(?:\.\d+).*(\d+)$', tcp_dump) 
result = list(result.groups()) 

print(result) 

輸出:

['192.168.0.15', '23.195.155.202', '0'] 
+0

有效IP,192.168.0.15,4341是一個mac地址。這是從一個TCP轉儲文件,這是什麼是連接到數據包時發送跨網絡和登錄到文件 –

+0

@AhmedImran,它只是一種轉儲格式,不是標準的,所以無效 – RomanPerekhrest