[Python] Python re.compile don't extract all group

Discussão em 'Python' iniciado por Stack, Maio 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using compile function to extract data from this text:

    Number of neighbors: 2

    AP Name AP IP Neighbor Name Neighbor IP Neighbor Port
    -------------------------------------------------------------------------------------------------------------------------------------------------------------
    ap001-w101-a91 1.2.3.4 sw0002-sw04-C22 2.3.4.5 GigabitEthernet1/0/1
    ap003-w4203-a28 2.3.2.2 sw002-sw02-c22.switch.local 3.2.3.2 GigabitEthernet1/0/17


    I have tested this regex with MULTILINE flag:

    ^(\S*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\S*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\S*)$


    But if I use on following code:

    parsed = re.compile(r'^(\S*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\S*)\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(\S*)$', re.M)


    I got 0 elements when I call findall function.

    Instead if I remove the final "(\S*)$" I got 2 elements.

    There is a solution in order to extract following data:

    hostname
    AP IP
    CDP Hostname
    CDP IP
    CDP Port

    Continue reading...

Compartilhe esta Página