Python 3 regex woes (parsing ISC DHCPD config)

Albert-Jan Roskam fomcl at yahoo.com
Mon Jan 12 22:58:25 CET 2015



----- Original Message -----

> From: Jason Bailey <jbailey at emerytelcom.com>
> To: python-list at python.org
> Cc: 
> Sent: Monday, January 12, 2015 7:20 PM
> Subject: Python 3 regex woes (parsing ISC DHCPD config)
> 
> 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?


Will the following work for you? My brain shuts down when I try to read your regex, but I believe you also used a non-greedy match.


Python 3.4.2 (default, Nov 20 2014, 13:01:11) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> cfg = """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;
...     }
...   }
... }"""
>>> import re
>>> re.findall(r"shared\-network (.+) \{?", cfg)
['My-Network-MOHE', 'My-Network-CDCO', 'My-Network-FECO']
>>> 



More information about the Python-list mailing list