Using dictionary to hold regex patterns?

André andre.roberge at gmail.com
Sun Nov 23 15:57:56 EST 2008


On Nov 23, 1:40 pm, Gilles Ganault <nos... at nospam.com> wrote:
> Hello
>
> After downloading a web page, I need to search for several patterns,
> and if found, extract information and put them into a database.
>
> To avoid a bunch of "if m", I figured maybe I could use a dictionary
> to hold the patterns, and loop through it:
>
> ======
> pattern = {}
> pattern["pattern1"] = ">.+?</td>.+?>(.+?)</td>"
> for key,value in pattern.items():
>         response = ">whatever</td>.+?>Blababla</td>"
>
>         #AttributeError: 'str' object has no attribute 'search'
>         m = key.search(response)
>         if m:
>                 print key + "#" + value
> ======
>
> Is there a way to use a dictionary this way, or am I stuck with
> copy/pasting blocks of "if m:"?
>
> Thank you.

Yes it is possible and you don't need to use pattern.items()...

Here is something I use (straight cut-and-paste):

    def parse_single_line(self, line):
        '''Parses a given line to see if it match a known pattern'''
        for name in self.patterns:
            result = self.patterns[name].match(line)
            if result is not None:
                return name, result.groups()
        return None, line


where self.patterns is something like
self.patterns={
'pattern1': re.compile(...),
'pattern2': re.compile(...)
}

The one potential problem with the method as I wrote it is that
sometimes a more generic pattern gets matched first whereas a more
specific pattern may be desired.

André



More information about the Python-list mailing list