V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Hakmor
V2EX  ›  问与答

如何用 python 正则匹配相应的内容呢?

  •  
  •   Hakmor · 2015-09-09 15:43:38 +08:00 · 1654 次点击
    这是一个创建于 3153 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个文本,可能是这样的形式:

    内网-10.0.0.0 外网-182.0.0.0
    外网-123.2.2.2 内网-10.2.2.2
    内网-10.3.4.5
    外网-182.1.1.1
    
    内网-10.0.1.1 外网-182.0.1.1
    

    请问怎么用正则匹配出内网后的 ip 地址呢?

    4 条回复    2015-09-10 10:09:11 +08:00
    imn1
        1
    imn1  
       2015-09-09 16:02:40 +08:00
    ^[^\d\.]+([\d\.]+)
    gongpeione
        2
    gongpeione  
       2015-09-09 16:13:48 +08:00
    import rem =
    text = "内网-10.0.0.1"
    m = re.findall (r"内网-(10\.(\d{1,3}\.){2}\d{1,3})", text )
    gongpeione
        3
    gongpeione  
       2015-09-09 16:14:42 +08:00   ❤️ 1
    import re
    text = "内网-10.0.0.1 ·····"
    m = re.findall (r"内网-(10\.(\d{1,3}\.){2}\d{1,3})", text )

    手滑
    Hakmor
        4
    Hakmor  
    OP
       2015-09-10 10:09:11 +08:00
    似乎输出格式还是不对

    我得到的是这样一个输出

    [('10.77.129.52', '129.')]

    text = "YY14072011 内网-10.77.129.52;管理卡-10.127.176.52;"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   857 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:43 · PVG 04:43 · LAX 13:43 · JFK 16:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.