从从文本fi提取IP的正则表达式中排除IP

2024-09-26 22:12:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我之前发布了一个关于这个的问题,但是忘记了另外一个案例。这是我的第一个问题:

Regex to include and exclude certain IPs

另一种情况是路由表中的这一行:

D*EX 0.0.0.0/0 [170/19664] via 10.10.10.1, 5d22h, Vlan10
           [170/19664] via 10.10.10.1, 5d22h, Vlan20

如何编辑正则表达式以从下面的正则表达式中排除0.0.0.0/0 IP:

(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)

我试过这些,但没有成功:

(?! 0.0.0.0/0)(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)
                                 AND
(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)(?! 0.0.0.0/0)

谢谢

达蒙


Tags: andtoincludeis情况viaexclude案例
1条回答
网友
1楼 · 发布于 2024-09-26 22:12:07

正如@Greg Hewgill所评论的,我认为您可以使用if语句。你知道吗

if ('0.0.0.0/0' not in text) and ('is variably' not in text):
    match = re.findall(r'(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b', text)

如果你真的想用正则表达式来解决它,这就是它。https://regex101.com/r/jTu8cj/2

(?!0\.0\.0\.0/0)(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\/(?:[\d]{1,3})\b(?! is variably)

# Positive
D       10.50.80.0/24 [90/3072] via 10.10.10.1, 3w6d, Vlan10
C       10.10.140.0/24 is directly connected, Vlan240
10.10.140.0/2
10.10.140.0/16
2.2.2.2/24
5.5.5.5.5/24

# Negative
10.0.0.0
     10.10.60.0/16 is variably subnetted, 58 subnets, 4 masks
0.0.0.0/0 [170/19664] via 10.10.10.1, 5d22h, Vlan10

相关问题 更多 >

    热门问题