Hakmor
V2EX  ›  问与答

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

  •  
  •   Hakmor · Sep 9, 2015 · 2431 views
    This topic created in 3922 days ago, the information mentioned may be changed or developed.

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

    内网-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 replies    2015-09-10 10:09:11 +08:00
    imn1
        1
    imn1  
       Sep 9, 2015
    ^[^\d\.]+([\d\.]+)
    gongpeione
        2
    gongpeione  
       Sep 9, 2015
    import rem =
    text = "内网-10.0.0.1"
    m = re.findall (r"内网-(10\.(\d{1,3}\.){2}\d{1,3})", text )
    gongpeione
        3
    gongpeione  
       Sep 9, 2015   ❤️ 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
       Sep 10, 2015
    似乎输出格式还是不对

    我得到的是这样一个输出

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

    text = "YY14072011 内网-10.77.129.52;管理卡-10.127.176.52;"
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1149 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 17:46 · PVG 01:46 · LAX 10:46 · JFK 13:46
    ♥ Do have faith in what you're doing.