2021年3月8日星期一

Return regular expression only one time

I am trying to have regular expression only print the result one time. Is there any suggestions? Since I want the code to read the entire text file, but there are many dates that are the same, but I just want the code to return that date one time only.

code:

import re  filename = set(open('wireshark.txt', 'r'))    pattern_object = re.compile(r'(\d\d\d\d-\d\d-\d\d)')  for line in filename:          match_object = pattern_object.search(line)          if match_object:                  regex = match_object.group(1)                  print(regex)  

Text file:

No.     Time                          Source                Destination           Protocol Length Info        2 2021-02-12 13:33:12.206424    192.168.1.151         172.217.10.46         QUIC     1392   Initial, DCID=e4267bae554f387d, PKN: 1, CRYPTO, PADDING    Frame 2: 1392 bytes on wire (11136 bits), 1392 bytes captured (11136 bits) on interface \Device\NPF_{28AA034F-AC94-4D4A-9CA9-9AEA5D0EF2C1}, id 0  Ethernet II, Src: Micro-St_0e:cd:34 (00:d8:61:0e:cd:34), Dst: Verizon_fb:8b:82 (20:c0:47:fb:8b:82)  Internet Protocol Version 4, Src: 192.168.1.151, Dst: 172.217.10.46  User Datagram Protocol, Src Port: 57189, Dst Port: 443  QUIC IETF    No.     Time                          Source                Destination           Protocol Length Info        3 2021-02-12 13:33:12.225610    172.217.10.46         192.168.1.151         QUIC     1392   Initial, SCID=e4267bae554f387d, PKN: 1, ACK, CRYPTO, PADDING    Frame 3: 1392 bytes on wire (11136 bits), 1392 bytes captured (11136 bits) on interface \Device\NPF_{28AA034F-AC94-4D4A-9CA9-9AEA5D0EF2C1}, id 0  Ethernet II, Src: Verizon_fb:8b:82 (20:c0:47:fb:8b:82), Dst: Micro-St_0e:cd:34 (00:d8:61:0e:cd:34)  Internet Protocol Version 4, Src: 172.217.10.46, Dst: 192.168.1.151  User Datagram Protocol, Src Port: 443, Dst Port: 57189  QUIC IETF    No.     Time                          Source                Destination           Protocol Length Info        4 2021-02-12 13:33:12.225989    192.168.1.151         172.217.10.46         TLSv1.2  146    Application Data  No.     Time                          Source                Destination           Protocol Length Info        4 2021-04-12 13:33:12.225989    192.168.1.151         172.217.10.46         TLSv1.2  146    Application Data  No.     Time                          Source                Destination           Protocol Length Info        4 2021-06-12 13:33:12.225989    192.168.1.151         172.217.10.46         TLSv1.2  146    Application Data  No.     Time                          Source                Destination           Protocol Length Info        4 2021-06-12 13:33:12.225989    192.168.1.151         172.217.10.46         TLSv1.2  146    Application Data  

Code execute output:

2021-02-12  2021-02-12  2021-02-12  2021-02-12  2021-02-12  2021-02-12  2021-04-12  2021-06-12  2021-06-12  

desire code execute output:

2021-02-12  2021-04-12  2021-06-12  
https://stackoverflow.com/questions/66539141/return-regular-expression-only-one-time March 09, 2021 at 08:28AM

没有评论:

发表评论