可以使用正则表达式来匹配 IPv4 地址格式:

import re

def is_valid_ipv4_address(address):
    # 使用正则表达式匹配 IPv4 地址格式
    pattern = r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$'
    match = re.match(pattern, address)
    if not match:
        return False
    # 检查每个数字是否在合法范围内
    for num in match.groups():
        if int(num) > 255:
            return False
    return True

使用示例:

print(is_valid_ipv4_address('192.168.0.1'))  # True
print(is_valid_ipv4_address('256.0.0.1'))    # False
print(is_valid_ipv4_address('192.168.0'))    # False
ipv4 格式的 ip 地址匹配提示 IP地址的范围是 0000 - 255255255255用Python代码编写

原文地址: http://www.cveoy.top/t/topic/buUd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录