簡體   English   中英

多個不同范圍的IP地址的正則表達式,需要對特定IP進行解析

[英]regular expression for multiple IP address with different range, need to pharse specific IP

我正在尋找正則表達式來驗證不同掩碼的 IP 地址

例子:

IP = [10.0.1.1 , 192.168.1.1, 200.1.1.1]

在這些 IP 中,我只需要為10.0.1.1解析(查找)IP。

嘗試使用此處找到的 python ip_address庫( https://docs.python.org/3/library/ipaddress.html

您可以嘗試做的是將它們全部解析為對象列表

result = list(map(ipaddress.ip_address, IP))
# then we filter depending on the bitmask here

與 ip 中的位數完全匹配的簡單正則表達式如下:

\\d{2}(\\.\\d{1}){3}

該正則表達式的Pythex

您應該提供有關您在 IP 地址中查找哪種模式的更多信息?

根據您當前的需要,您可以使用它:

import re
pattern=r'\d{2}(?=[.]).\d{1}([.]\d{1})(\1)'

string="IP = [10.0.1.1 , 192.168.1.1, 200.1.1.1]"

match=re.search(pattern,string)

print(match.group())

輸出:

10.0.1.1

暫無
暫無

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

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