Python Forum
re.finditer issue, output is blank - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: re.finditer issue, output is blank (/thread-15941.html)



re.finditer issue, output is blank - anna - Feb-07-2019

Hi All,

trying below code, running with out error, however output is blank.


import re
from ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj

with open("conf.txt") as file:
   data = file.read()
interface_ips = re.finditer(r"^(interface (?P<intf_name>.*)\n)"
                            r"( .*\n)*"
                            r"( ip address (?P<ipv4_address>\S+) (?P<subnet_mask>\S+))\n"
                            r"( description (?P<description>.*))\n",
                                data,re.MULTILINE)
for intf_ip in interface_ips:
    print("%s %s/%s %s" % (intf_ip.group("intf_name"),
                           intf_ip.group("ipv4_address"),
                           intf_ip.group("subnet_mask"),
                           intf_ip.group("description")))
below is my input file.


interface Eth-Trunk1.120
vlan-type dot1q 120
description EXT_COGENT E SERVICES PRIVATE LIMITED_12005744750_50MB
ip address 111.93.43.217 255.255.255.252
traffic-policy INFRA-ACL inbound
qos-profile 50Mbps inbound identifier none
qos-profile 50Mbps outbound identifier none
statistic enable
#
interface Eth-Trunk1.123
vlan-type dot1q 123
description EXT_ILL_SARALA-HANDICRAFTS_PANIPAT_5018027739
ip address 182.156.211.161 255.255.255.252
traffic-policy INFRA-ACL inbound
user-queue cir 10240 pir 10400 inbound
user-queue cir 10240 pir 10400 outbound
statistic enable


RE: re.finditer issue, output is blank - stranac - Feb-07-2019

In your regex, ip address comes before description, in the input file, it's the opposite.
Also, if this is a common file format, you might want to try looking up an existing parser instead of doing your own regex-based parsing.