【发布时间】:2020-11-22 07:51:57
【问题描述】:
下面的正则表达式 (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
【问题讨论】:
-
只需使用列入黑名单的 IP 列表过滤结果即可。
-
@WiktorStribiżew 你能举个例子吗?
标签: list python-3.7