Hashtable/dictionary/map lookup with regular expressions

前端 未结 19 1545
难免孤独
难免孤独 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:24

    If you have a small set of possible inputs, you can cache the matches as they appear in a second dict and get O(1) for the cached values.

    If the set of possible inputs is too big to cache but not infinite, either, you can just keep the last N matches in the cache (check Google for "LRU maps" - least recently used).

    If you can't do this, you can try to chop down the number of regexps you have to try by checking a prefix or somesuch.

提交回复
热议问题