Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!feeder3.cambriumusenet.nl!feed.tweak.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.81.MISMATCH!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Question': 0.05; 'ugly': 0.07; '(1,': 0.09; '0))': 0.09; '0),': 0.09; 'literal': 0.09; 'optional': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:# 60': 0.09; 'python.': 0.11; 'comma': 0.16; 'commented': 0.16; 'expressions,': 0.16; 'hex': 0.16; 'pprint': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'wrote:': 0.16; 'stick': 0.18; 'second': 0.24; 'import': 0.24; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; 'header:X -Complaints-To:1': 0.26; 'disk': 0.27; 'looks': 0.29; 'regular': 0.29; 'boundary': 0.29; 'chase': 0.29; 'grouping': 0.29; 'whitespace': 0.29; 'anywhere': 0.30; "i'd": 0.31; 'guess': 0.31; 'problem': 0.33; 'but': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'things': 0.38; 'version': 0.38; 'stuff': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'capture': 0.66; 'sector': 0.72; 'received:12': 0.81; '12:20': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Emile van Sebille Subject: Re: Question about regular expression Date: Wed, 30 Sep 2015 20:58:10 -0700 References: <811788b6-9955-4dcc-bf49-9647891d17ec@googlegroups.com> <20150930142015.4045c931@bigbox.christie.dr> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: www.westernstatesglass.com User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: <20150930142015.4045c931@bigbox.christie.dr> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1443671907 news.xs4all.nl 23838 [2001:888:2000:d::a6]:36012 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:97263 On 9/30/2015 12:20 PM, Tim Chase wrote: > On 2015-09-30 11:34, massi_srb@msn.com wrote: >> I guess this problem can be tackled with regular expressions, b > ... However, if you *want* to do it with > regular expressions, you can. It's ugly and might be fragile, but > > ############################################################# > import re > s = "name1 name2(1) name3 name4 (1, 4) name5(2) ..." > r = re.compile(r""" > \b # start at a word boundary > (\w+) # capture the word > \s* # optional whitespace > (?: # start an optional grouping for things in the parens > \( # a literal open-paren > \s* # optional whitespace > (\d+) # capture the number in those parens > (?: # start a second optional grouping for the stuff after a comma > \s* # optional whitespace > , # a literal comma > \s* # optional whitespace > (\d+) # the second number > )? # make the command and following number optional > \) # a literal close-paren > )? # make that stuff in parens optional > """, re.X) > d = {} > for m in r.finditer(s): > a, b, c = m.groups() > d[a] = (int(b or 0), int(c or 0)) > > from pprint import pprint > pprint(d) > ############################################################# :) > > I'd stick with the commented version of the regexp if you were to use > this anywhere so that others can follow what you're doing. ... and this is why I use python. That looks too much like a hex sector disk dump rot /x20. :) No-really-that's-sick-ly yr's, Emile