Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #83621

Python 3 regex woes (parsing ISC DHCPD config)

Date 2015-01-12 11:20 -0700
From Jason Bailey <jbailey@emerytelcom.com>
Subject Python 3 regex woes (parsing ISC DHCPD config)
Newsgroups comp.lang.python
Message-ID <mailman.17631.1421087744.18130.python-list@python.org> (permalink)

Show all headers | View raw


Hi all,

I'm working on a Python _3_ project that will be used to parse ISC DHCPD 
configuration files for statistics and alarming purposes (IP address 
pools, etc). Anyway, I'm hung up on this one section and was hoping 
someone could provide me with some insight.

My script first reads the DHCPD configuration file into memory - 
variable "filebody". It then utilizes the re module to find the 
configuration details for the wanted "shared network".

The config file might look something like this:

######################################

shared-network My-Network-MOHE {
   subnet 192.168.0.0 netmask 255.255.248.0 {
     option routers 192.168.0.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.0.20 192.168.7.254;
     }
   }
}

shared-network My-Network-CDCO {
   subnet 192.168.8.0 netmask 255.255.248.0 {
     option routers 10.101.8.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.8.20 192.168.15.254;
     }
   }
}

shared-network My-Network-FECO {
   subnet 192.168.16.0 netmask 255.255.248.0 {
     option routers 192.168.16.1;
     option tftp-server-name "192.168.90.12";
     pool {
       deny dynamic bootp clients;
       range 192.168.16.20 192.168.23.254;
     }
   }
}

######################################

Suppose I'm trying to grab the shared network called "My-Network-FECO" 
from the above config file stored in the variable 'filebody'.

First I have my variable 'shared_network' which contains the string 
"My-Network-FECO".

I compile my regex:
m = re.compile(r"^(shared\-network (" + re.escape(shared_network) + r") 
\{((\n|.|\r\n)*?)(^\}))", re.MULTILINE|re.UNICODE)

I search for regex matches in my config file:
m.search(filebody)

Unfortunately, I get no matches. From output on the command line, I can 
see that Python is adding extra backslashes to my re.compile string. I 
have added the raw 'r' in front of the strings to prevent it, but to no 
avail.

Thoughts on this?

Thanks

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Python 3 regex woes (parsing ISC DHCPD config) Jason Bailey <jbailey@emerytelcom.com> - 2015-01-12 11:20 -0700
  Re: Python 3 regex woes (parsing ISC DHCPD config) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 12:43 +0100
    Re: Python 3 regex woes (parsing ISC DHCPD config) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-13 17:09 +0100

csiph-web