python匹配ip正则

python匹配ip正则

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
import re  
ip_str = "asdad1.1.1.1sdfwe2.6.5.7sdfsf2.3.94.5sdf200.198.25.0"  
res = []  
for i in range(1):  
  
    ip_t = True  
    m_ind = 0  
    while ip_t:  
        m = re.search(r"(((2([0-4]\d|5[0-5])|((1\d|[1-9])?\d)|\d)\.){3}(2([0-4]\d|5[0-5])|(1\d|[1-9])?\d|\d))",ip_str[m_ind:])  
        if m:  
            msum = m.group()  
            res.append(msum)  
            m_ind = ip_str.index(msum) + len(msum) -1  
        else:  
            ip_t = False  
  
print(res)  
#print(msum,m_ind)  

思路

图片有错误,但思路正确,代码和图片对比,放这个是为了对比思路,赞这个图片的人的思路,着实牛 

 shell grep 过滤所有ip

cat abc | grep -Po "(^10\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)|(^172\.(1[6789]|2[0-9]|3[01])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)|(^192\.168\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[0-9])$)"

 

posted @ 2016-07-01 23:41  我当道士那儿些年  阅读(984)  评论(0编辑  收藏  举报