Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83640
| Date | 2015-01-12 21:58 +0000 |
|---|---|
| From | Albert-Jan Roskam <fomcl@yahoo.com> |
| References | <54B41073.30902@emerytelcom.com> |
| Subject | Re: Python 3 regex woes (parsing ISC DHCPD config) |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17643.1421100206.18130.python-list@python.org> (permalink) |
----- Original Message -----
> From: Jason Bailey <jbailey@emerytelcom.com>
> To: python-list@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']
>>>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Python 3 regex woes (parsing ISC DHCPD config) Albert-Jan Roskam <fomcl@yahoo.com> - 2015-01-12 21:58 +0000
csiph-web