• python 判断字符串是否为(或包含)IP地址


    下面是某个字符串是否为IP地址
    import re

    def isIP(str):
    p = re.compile('^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$')
    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
    def ip_exist_two(one_url):
    compile_rule = re.compile(r'(?<![.d])(?:d{1,3}.){3}d{1,3}(?![.d])')
    match_list = re.findall(compile_rule, one_url)
    if match_list:
    print (match_list)
    else:
    print('missing................')
    def ip_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_url in ip_list:
    ip_exist_one(one_url)
    print ('****************************************************')
    for one_url in ip_list:
    ip_exist_two(one_url)
    主要参考代码链接:https://www.jb51.net/article/141424.htm
    https://www.cnblogs.com/ccz320/p/6536993.html
  • 相关阅读:
    关于截取字符串substr和substring两者的区别
    cancelbubble和stoppraopagation区别
    字符串转json以及获取域名的参数
    JSON.parse()和JSON.stringify()
    jq获取今天、昨天、一周时间
    浏览器是如何渲染网页的
    React入门一
    Json 查看Json的插件
    wireshark
    iOS开发 Android开发 移动Web开发
  • 原文地址:https://www.cnblogs.com/kjkj/p/10572046.html
Copyright © 2020-2023  润新知