matching and extracting...

Andrew Dalke adalke at mindspring.com
Wed Jan 15 04:53:52 EST 2003


Cliff Wells wrote:
> This is a couple of lines shorter and now my design is perfect <wink>:

I've heard that before somewhere ...  :)

> 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.

					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list