美文网首页
判断一个字符串为IP 地址

判断一个字符串为IP 地址

作者: 金色森林_0d96 | 来源:发表于2021-03-08 00:00 被阅读0次

    不用正则:

    使用正则(转载)

    import re

    defisIP(str):

    p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')

    if p.match(str):

    return True

    else:

    return False

    myStr ="10.69.36.95"

    if isIP(myStr):

    print(myStr,"it is a IP!")

    else:

    print(myStr,"it is not a IP!")

    下面是某个字符串是否包含ip地址:

    import re

    defip_exist_two(one_url):

    compile_rule = re.compile(r'(?

    match_list = re.findall(compile_rule, one_url)

    if match_list:

    print (match_list)

    else:

    print('missing................')

    defip_exist_one(one_url):

    compile_rule = re.compile(r'\d+[\.]\d+[\.]\d+[\.]\d+')

    match_list = re.findall(compile_rule, one_url)

    if match_list:

    print( match_list)

    else:

    print ('missing................')

    if __name__ =='__main__':

    ip_list = ['http://101.23.45.67/sd/sd.html','http://www.baidu.com',

    'http://34.54.65.3/dsdfjkk.htm','http://dhj.fdjjd.com/78078979/dsdfjkk.htm']

    for one_urlin ip_list:

    ip_exist_one(one_url)

    print ('****************************************************')

    for one_urlin ip_list:

    ip_exist_two(one_url)

    主要参考代码链接:https://www.jb51.net/article/141424.htm

    https://www.cnblogs.com/ccz320/p/6536993.html

    相关文章

      网友评论

          本文标题:判断一个字符串为IP 地址

          本文链接:https://www.haomeiwen.com/subject/vwvuqltx.html