Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'yet.': 0.04; 'output': 0.05; '"""': 0.07; 'skip:\\ 20': 0.07; 'subject:help': 0.08; 'initiator': 0.09; 'parsed': 0.09; 'parsing': 0.09; 'python': 0.11; 'adjustments': 0.16; 'blocks': 0.16; 'format:': 0.16; 'from:addr:lists': 0.16; 'port,': 0.16; "r'''": 0.16; 're.compile(': 0.16; 'subject:?)': 0.16; 'subject:Problem': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'tcp': 0.16; 'wrote:': 0.18; 'command': 0.22; 'import': 0.22; 'skip:+ 20': 0.22; 'print': 0.22; 'skip': 0.24; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'code': 0.31; 'skip:# 10': 0.33; 'could': 0.34; 'subject: (': 0.35; 'connection': 0.35; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'set.': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'expression': 0.60; 'logged': 0.60; 'skip:2 20': 0.60; 'skip:i 50': 0.60; 'name:': 0.61; 'received:173': 0.61; 'back': 0.62; 'address': 0.63; 'header :Message-Id:1': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'received:phx3.secureserver.net': 0.65; 'received:prod.phx3.secureserver.net': 0.65; 'portal': 0.68; 'default': 0.69; 'lastly,': 0.84; 'persistent': 0.84; '2013,': 0.91; 'received:173.201': 0.91 Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 6.2 \(1499\)) Subject: Re: Problem creating a regular expression to parse open-iscsi, iscsiadm output (help?) From: Kevin LaTona In-Reply-To: Date: Thu, 13 Jun 2013 07:42:07 -0700 Content-Transfer-Encoding: quoted-printable References: To: python-list@python.org X-Mailer: Apple Mail (2.1499) X-Mailman-Approved-At: Thu, 13 Jun 2013 17:17:55 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371136676 news.xs4all.nl 15987 [2001:888:2000:d::a6]:53788 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47977 On Jun 12, 2013, at 5:59 PM, rice.cruft@gmail.com wrote: > I am parsing the output of an open-iscsi command that contains several = blocks of data for each data set. Each block has the format: > Lastly, a version of this regex as a non-VERBOSE expression works as = expected.. Something about re.VERBOSE... ???? Snip With the following code tweaks in Python 2.7.2, I find it works with = VERBOSE for me, but not without. I would say the regex could still use some more adjustments yet. -Kevin import re inp =3D""" Target: iqn.1992-04.com.emc:vplex-000000008460319f-0000000000000007 Current Portal: 221.128.52.224:3260,7 Persistent Portal: 221.128.52.224:3260,7 ********** Interface: ********** Iface Name: default Iface Transport: tcp Iface Initiatorname: iqn.1996-04.de.suse:01:7c9741b545b5 Iface IPaddress: 221.128.52.214 Iface HWaddress: Iface Netdev: SID: 154 iSCSI Connection State: LOGGED IN iSCSI Session State: LOGGED_IN Internal iscsid Session State: NO CHANGE """ regex =3D re.compile( r''' # Target name, iqn Target:\s+(?P\S+)\s* # Target portal \s+Current\sPortal:\s* (?P\w+\.\w+\.\w+\.\w+):(?P\d+),(?P\d+) # skip lines... [\s\S]*? # Initiator name, iqn Iface\s+Initiatorname:\s+(?P\S+)\s* # Initiator port, IP address Iface\s+IPaddress:\s+(?P\S+) # skip lines... [\s\S]*? # Session ID SID:\s+(?P\d+)\s* # Connection state iSCSI\ +Connection\ +State:\s+(?P\w+\s*\w*) [\s\S]*? # Session state iSCSI iSCSI\s+Session\s+State:\s+(?P\w+)\s* # Session state Internal Internal\s+iscsid\s+Session\s+State:.*\s+(?P\w+\s\w+) ''', re.VERBOSE|re.MULTILINE) myDetails =3D [ m.groupdict() for m in regex.finditer(inp)][0] for k,v in myDetails.iteritems(): print k,v #************* If you want just the values back in the order parsed this will work for = now. for match in regex.findall(inp): for item in range(len(match)): print match[item]