下面的正则表达式(ip_regex)将找到所有IP地址,但是有一长串我不想匹配的IP地址。例如,我需要过滤私有IP地址以及其他公共IP。我该如何添加此正则表达式来实现此目的?
import re
fh = "some file.txt"
fh2 = "some file2.txt"
ip_regex = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
for line in fh:
line = line.strip()
match = ip_regex.findall(line)
if match:
for (ip) in match:
print('\n'.join(match), file=fh2)
else:
pass
答案 0 :(得分:0)
我建议您使用ìp
变量来构建IPAddress[1]
对象。然后,您可以在其上调用isPrivate()
方法,或询问它是否包含在IPv4Network
中。这会更加冗长,但是排除随机子网的常规正则表达式将以可读性较低的代码结尾。
在修改后的代码段中,我还更改了一条对我来说似乎是个错误的行(在此处进行了注释)。
import re
from ipaddress import IPv4Address
fh = 'ips.txt'
fh2= open ('filtereds.txt', 'w')
ip_regex = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})")
with open (fh, "r") as myfile:
data=myfile.readlines()
for line in data:
line = line.strip()
match = ip_regex.findall(line)
if match:
for (ip) in match:
ipaddress = IPv4Address(ip)
if ipaddress.is_global: # and other criterias
# print('\n'.join(match), file=fh2) this would print all the line (with other IPs that does not fit criteria)
print(ip, file=fh2) #this would print only the matched IP
else:
pass