import re
import os
with open("1.txt","r",encoding='utf8') as f:
for line in f:
print(type(line))
print(line)
ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
print(ip_address)
输出:
<class 'str'>
https VPN: public --> public Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443
['192.160.227.127:24158']
<class 'str'>
udp VPN: public --> public Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027
['192.160.226.25:25668']
<class 'str'>
udp VPN: public --> public Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999
逐行文件,单行存储,正则匹配,匹配到放到一个列表中
适合读取大文件
import re
import os
with open("1.txt","r",encoding='utf8') as f:
line=f.read()
print(type(line))
print(line)
ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
print(ip_address)
输出
<class 'str'>
https VPN: public --> public Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443
udp VPN: public --> public Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027
udp VPN: public --> public Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999
['192.160.227.127:24158', '192.160.226.25:25668', '192.160.224.1:27636']
读取所有文件,分行存储,正则逐行匹配,匹配到放到一个列表中
不适合读取大文件
import re
import os
with open("1.txt","r",encoding='utf8') as f:
lines=f.readlines()
print(type(lines))
print(lines)
for line in lines:
print(type(line))
print(line)
ip_address=re.findall(r".*?(192.\d+.\d+.\d+:\d+).*",line)
print(ip_address)
<class 'list'>
[' https VPN: public --> public Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443 \n', ' udp VPN: public --> public Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027 \n', ' udp VPN: public --> public Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999 \n']
<class 'str'>
https VPN: public --> public Remote 10.1.164.170:54472[192.160.227.127:24158] --> 47.106.88.76:443
['192.160.227.127:24158']
<class 'str'>
udp VPN: public --> public Remote 10.2.35.192:8567[192.160.226.25:25668] --> 117.147.3.250:1027
['192.160.226.25:25668']
<class 'str'>
udp VPN: public --> public Remote 10.0.160.248:51559[192.160.224.1:27636] --> 47.104.73.216:9999
['192.160.224.1:27636']