matching and extracting...

Cliff Wells clifford.wells at attbi.com
Wed Jan 15 11:16:56 EST 2003


On Wed, 2003-01-15 at 01:53, Andrew Dalke wrote:
> Cliff Wells wrote:
> > This is a couple of lines shorter and now my design is perfect <wink>:
> 
> I've heard that before somewhere ...  :)

Now you slander me by insinuating that I'm a plagiarist?  I'm not
talking to you any more <wink>

> 
> > import re
> > 
> > class matchseq:
> >     def __init__(self, sequence):
> >         self.items = []
> >         exp = ""
> >         for item in sequence.split():
> >             if exp:
> >                 exp += " "
> >             k, v = item.split('-')
> >             exp += "(?P<%s>%s)" % (k, ["\D*\d*", k][v == 'p'])
> 
> Pedantically speaking, you should worry about a "k" with embedded
> re metacharacters in it.  For example, have an 're.escape(k)' in case
> there's ever a term like "a.1" and use a group name which you
> construct yourself, then do a mapping from groupdict() to get the
> requested mapping.
>
> Though given the lack of spec, my qualifiers are irrelevant.

There are a couple of other potential shortcomings as well, but I
decided that this satisfied the OP's exact request.  Handling things
outside of that is Left As An Exercise(tm).

-- 
Cliff Wells <clifford.wells at attbi.com>






More information about the Python-list mailing list