Hashtable/dictionary/map lookup with regular expressions

前端 未结 19 1288
难免孤独
难免孤独 2021-02-01 05:36

I\'m trying to figure out if there\'s a reasonably efficient way to perform a lookup in a dictionary (or a hash, or a map, or whatever your favorite language calls it) where the

19条回答
  •  迷失自我
    2021-02-01 06:19

    What happens if you have a dictionary such as

    regex_dict = { re.compile("foo.*"): 5, re.compile("f.*"): 6 }
    

    In this case regex_dict["food"] could legitimately return either 5 or 6.

    Even ignoring that problem, there's probably no way to do this efficiently with the regex module. Instead, what you'd need is an internal directed graph or tree structure.

提交回复
热议问题