簡體   English   中英

通過python regex忽略IP地址的第一個八位字節中的這兩個數字(以127或0開頭)

[英]Ignore these two numbers (starts with 127 or 0) in the first octet of the ip address through python regex

我有一個存儲在文本文件中的IP列表以及一些其他數據,這些數據我試圖僅從其中提取有效IP。 在這里,我有一些IP,例如0.0.0.0和localhost IP(以127 ...開頭),我正嘗試使用正則表達式消除它們。 這是我想到的過濾器0.0.0.0 IP的正則表達式模式,但無法有效刪除127 .. * IP。

import re
with open("data","r") as f:
    for line in f:
        test = re.search(r'(?!0|127)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line)
        if test:
            print(test.group(0))

IP位於文本文件中,如下所示:

127.3.65.7
alkjgfbvui vluiybr vk ru r127.0.0.1fal;iufnaw  waoun
12.0.1.5
mjhgvjg0.0.0.0kjuycuj
0.0.0.0
0.0.0.0
gare bloing r pgnao wyin212.2.174.64
207.71.31.224
awuie nvp; vwa rv;awiu n ;lkirjght94.206.93.104ta;wourit mrt'
172.20.128.1
172.20.164.207
172.20.164.203
172.20.164.209
1.8.0.144

我得到以下輸出,在其中您可以看到僅刪除第一個數字“ 1”也可以打印以127開頭的IP

27.3.65.7
27.0.0.1
12.0.1.5
212.2.174.64
207.71.31.224
94.206.93.104
172.20.128.1
172.20.164.207
172.20.164.203
172.20.164.209
1.8.0.144

試試正則表達式: (?<!\\d)(?!0|127)\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}

演示

說明:

在\\ d后面加上否定的外觀以消除以27開頭的匹配項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM