#!/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)
图片有错误,但思路正确,代码和图片对比,放这个是为了对比思路,赞这个图片的人的思路,着实牛
<img src="https://img-blog.csdn.net/20160519230618480?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐