Romain Picot
Romain Picot

Reputation: 255

How to use regex as dictionary's key?

I've to treat a file using a python script. The file is an automatic creation so some character can be added before and after the key. So I wanna use regex to match them (the middle part is always the same and their is any trouble to do it):

def dict(x):
 return{
  '.*key1.*': 'blabla',
  '.*key2.*': 'thing'
 }.get(x,'default')

This is not working because ".*" represent character.

How can I do to perform this?

Upvotes: 1

Views: 1331

Answers (2)

uranusjr
uranusjr

Reputation: 1459

You need to match the file yourself. get can't do that for you.

What you want might be something like

def get_content(filename):
    lookup = (
        (r'.*key1.*', 'blahblah',),
        (r'.*key2.*', 'thing',),
    )
    for pattern, content in lookup:
        if re.match(pattern, filename):
            return content

There are multiple things here that can be optimised, but the idea is there.


Edit:

As pointed out by mskimm and Grijesh Chauhan, the code attached in the question has two additional problems that need pointing out:

  • Do not use dict as a function or variable name since it is the name of a built-in type.
  • For regular expression patterns, use raw string literals (literals with prefix r, r'like this') instead of standard string literals ('like this') since regex patterns use characters that require escaping way too often.

The code in this answer reflects the above two points.

Upvotes: 1

emesday
emesday

Reputation: 6186

Alternatively, How about using this RegexDict

class RegexDict(dict):
    import re
    def __init__(self, *args, **kwds):
        self.update(*args, **kwds)

    def __getitem__(self, required):
        for key in dict.__iter__(self):
            if self.re.match(key, required):
                return dict.__getitem__(self, key)
        return dict.__getitem__(self, key) # redundancy but it can handle exceptions.

regex_dict = RegexDict({'.*key1.*':'blabla', '.*key2.*':'thing'})

print regex_dict['foo_key1_bar'] # blabla
print regex_dict['foo_key2_bar'] # thing

Upvotes: 2

Related Questions