Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'output': 0.05; 'string.': 0.05; 'subject:Python': 0.06; 'deny': 0.07; 'matches': 0.07; 'skip:\\ 20': 0.07; 'suppose': 0.07; 'string': 0.09; 'etc).': 0.09; 'skip:# 30': 0.09; 'subject:parsing': 0.09; 'python': 0.11; 'stored': 0.12; "'r'": 0.16; 'backslashes': 0.16; 'regex,': 0.16; 'prevent': 0.16; 'variable': 0.18; 'all,': 0.19; 'module': 0.19; 'trying': 0.19; 'thoughts': 0.19; 'command': 0.22; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'this?': 0.23; 'config': 0.24; 'parse': 0.24; 'script': 0.25; 'purposes': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; "i'm": 0.30; "skip:' 10": 0.31; 'you?': 0.31; 'file:': 0.31; 'file': 0.32; 'front': 0.32; 'option': 0.32; 'linux': 0.33; '-----': 0.33; 'monday,': 0.33; 'raw': 0.33; 'could': 0.34; 'subject: (': 0.35; 'something': 0.35; 'but': 0.35; 'january': 0.37; 'email addr:python.org': 0.37; 'project': 0.37; 'jason': 0.38; 'nov': 0.38; 'skip:[ 10': 0.38; 'to:addr:python- list': 0.38; 'files': 0.38; 'subject:': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'received:67.195': 0.60; 'received:98.137': 0.60; 'range': 0.61; 'first': 0.61; 'address': 0.63; 'grab': 0.64; 'provide': 0.64; 'more': 0.64; 'email name:python-list': 0.65; 'details': 0.65; 'header:Reply-To:1': 0.67; 'believe': 0.68; 'brain': 0.68; 'line,': 0.68; 'reads': 0.68; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'skip:r 30': 0.69; 'hoping': 0.75; '2014,': 0.84; '2015': 0.84; 'bailey': 0.84; 'clients;': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1421100086; bh=rctoLYCX61dakriSusS3/2CIxd5gb3r8h7tWU0lOSPs=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=fwnZeK09IxvLXN/Um3XYmYpuXKbDZnXI2YChsyfGRhfoJTqoR9fP+wK6U0k/MD9+JMY0Sa5lhDwF7Ofq5OMnYrzbCaYaplJUW//70+YpZy84tvzjsZ3IJbzjAgpyD7mXnL0C4tAfIAGW7AuJiDTdLnypbZd2pA0JjGg5fwRbtnjZNjWY+HzuiD3z10W/cYWVHuCdAPzMT6swxJDO4JrkmPTRMZ/UmIROGCILCGTyAVm/40DZ7vFrP3X14xqzjjd9fDSfDlX6+NW7KOIvi8kRkryAm7FVX0zWk/bPTELJnW/ZX5flqkaG6hNRcF03OZaKtvQeSHBL3IvMY7pMJJZa8Q== DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s2048; d=yahoo.com; b=BeykUdkL4z6QCTf3f6Ky3eXF3CtDDRMfY86AIm2QdpU9ecjJxPNFJV1s6s+HRPcN/FUHgPg3wNx/2Ar+4bncdYE2ElgR6pwOXdWq6OelWV9e9IwHT6A5A8igBfA5rPpcaPejDoj8Is2vP2AKV/v/6lGQ32pmcPye4YP6pCk/3x47KrQDCteh1wxXMSg1oRZaqX5ihLvFl51EgOrfrhyr3EhsQlqNmN56SDwLwU9e64YMelLTo1K6hCFNNdlBuutV7GW1n+RUVh37ZddDfD4Se2IWLjVQpjS0JLcDD9jXm3CHuhv/xQ5LOp3mqU3Ehba/Cx7OMu1nOylBEO94kaJYSA==; X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 34946.30292.bm@omp1019.mail.gq1.yahoo.com X-YMail-OSG: CVLuJSMVM1nfwqpzFnf6QWl_lKWy4ywZJy1oE9CoS3Pyo0d.mLf9AeTzKZ.mhpj ytXvevFtGUpMvPeJWRYtLFHQFCT5Ugv.3ux6Rh5y0_SX_OVAWU1Vi.GSqNrsykNIcGMZQiHkMuH7 RoTtp.jIgGtpkjGV0ePZiVnPp.0sd_7Tzio1dcPLMt0jT4TnKMr1UKrqp1k8AIK.Qb9DocHFzkER TrcC_a9Mm3OwVoLKm1Hxo7cNHbY438NGH2YxXRlpHxhGpQPEViApTuYMw9b7UNE9UlgbHrptFEIf EuQ0D4ezh991k5CJCWXQv7hYht29VKF4dTuvRWFNAv_go0dGhSJyOFceMHWlysHGrFJFcO47MDWq dSG0Bp9cuIreyTlqFcf5dG8Hg.5Qxj_XRlbrP2PIbs_VRGH5XX4an4hiYd6MF7dJr5XNbQPA4xoY TlSnDYzB14BjMDBFl2iHkd8hsfaTIfFPWLDPBQNHNMZwPsSbX7G9R0iiroE3ZxLl.4.CHtNBKxJF nOJAQRXIAJUBKdw-- Date: Mon, 12 Jan 2015 21:58:25 +0000 (UTC) From: Albert-Jan Roskam To: Jason Bailey , "python-list@python.org" In-Reply-To: <54B41073.30902@emerytelcom.com> References: <54B41073.30902@emerytelcom.com> Subject: Re: Python 3 regex woes (parsing ISC DHCPD config) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Albert-Jan Roskam List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 125 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421100206 news.xs4all.nl 2871 [2001:888:2000:d::a6]:35654 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83640 ----- Original Message ----- > From: Jason Bailey > 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'] >>>