如何匹配以逗号分隔的字符串中的 IP 地址

How to match IP addresses in a comma-separated string

代码如下,但是有一个问题:"255.255.255.256"会被处理成"255.255.255.25"

import re

ip_pattern = re.compile(r"((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)")

def get_ip_by_regex(ip_str):
    """Match IP from the given text and return

    :param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
    :type ip_str: string
    :return: IP LIST ["255.255.255.255"]
    :rtype: list[string]
    """
    ret = []
    for match in ip_pattern.finditer(ip_str):
        ret.append(match.group())
    return ret

如果我传递 255.255.255.255,255.255.255.256,260.255.255.255 字符串,我希望得到 ["255.255.255.255"] 作为结果。

你忘记拆分了。为避免重叠,我建议先拆分。然后你可以 matchfinditer。 这是具有查找逻辑的正确 IP 模式:

r"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"


def get_ip_by_regex(ip_str):
    """Match IP from the given text and return

    :param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
    :type ip_str: string
    :return: IP LIST ["255.255.255.255"]
    :rtype: list[string]
    """
    ret = []
    for p in ip_str.split(','):
        for match in ip_pattern.finditer(ip_str):
            ret.append(match.group())
    return ret

您想实施 逗号边界(?<![^,])(?![^,]):

ip_pattern = re.compile(r"(?<![^,])(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?![^,])")

参见regex demo

详情

  • (?<![^,]) - 一个负向后视,它匹配的位置不是紧接在逗号之外的字符前面(即必须有一个逗号或紧邻当前位置左侧的字符串开头)
  • (?![^,]) - 一个否定前瞻,它匹配一个位置后没有紧跟一个逗号以外的字符(即当前位置右侧必须有一个逗号或字符串结尾)。

参见 Python demo:

import re

ip_pattern = re.compile(r"(?<![^,])(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?![^,])")

def get_ip_by_regex(ip_str):
    """Match IP from the given text and return

    :param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
    :type ip_str: string
    :return: IP LIST ["255.255.255.255"]
    :rtype: list[string]
    """
    return ip_pattern.findall(ip_str)

print(get_ip_by_regex('255.255.255.255,255.255.255.256,260.255.255.255'))
# => ['255.255.255.255']

有字边界:

import re

ip_pattern = re.compile(r"\b((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)\b")

def get_ip_by_regex(ip_str):
    """Match IP from the given text and return

    :param ip_str: like "255.255.255.255,255.255.255.256,260.255.255.255"
    :type ip_str: string
    :return: IP LIST ["255.255.255.255"]
    :rtype: list[string]
    """
    ret = []
    for match in ip_pattern.finditer(ip_str):
        ret.append(match.group())
    return ret

print(get_ip_by_regex("255.255.255.255,255.255.255.256,260.255.255.255"))

Python code