python 判断字符串是否是IP

正则验证IP字符串

import re
def judge_ip(one_str):
    """
    正则匹配方法
    判断一个字符串是否是合法IP地址
    """
    compile_ip = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
    if compile_ip.match(one_str):
        return True
    else:
        return False

使用方法:

judge_ip('1.1.1.1') 返回True
posted @ 2023-01-06 16:10  yingzi__block  阅读(225)  评论(0编辑  收藏  举报