Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #97263

Re: Question about regular expression

From Emile van Sebille <emile@fenx.com>
Subject Re: Question about regular expression
Date 2015-09-30 20:58 -0700
References <811788b6-9955-4dcc-bf49-9647891d17ec@googlegroups.com> <20150930142015.4045c931@bigbox.christie.dr>
Newsgroups comp.lang.python
Message-ID <mailman.281.1443671907.28679.python-list@python.org> (permalink)

Show all headers | View raw


On 9/30/2015 12:20 PM, Tim Chase wrote:
> On 2015-09-30 11:34, massi_srb@msn.com wrote:
<snip>
>> 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



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Question about regular expression massi_srb@msn.com - 2015-09-30 11:34 -0700
  Re: Question about regular expression Emile van Sebille <emile@fenx.com> - 2015-09-30 11:50 -0700
  Re: Question about regular expression Tim Chase <python.list@tim.thechases.com> - 2015-09-30 14:20 -0500
  Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-30 23:30 +0000
    Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-02 18:25 +0000
  Re: Question about regular expression Emile van Sebille <emile@fenx.com> - 2015-09-30 20:58 -0700
  Re: Question about regular expression Tim Chase <python.list@tim.thechases.com> - 2015-10-01 07:39 -0500
  Re: Question about regular expression Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-10-01 15:53 +0000
    Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-01 21:41 +0000
  Re: Question about regular expression Denis McMahon <denismfmcmahon@gmail.com> - 2015-10-01 21:31 +0000

csiph-web