开发者

Collection with RegEx lookup.... does it exist

开发者 https://www.devze.com 2023-01-26 10:38 出处:网络
Does there exist some kind of collection (key, value) where values can be found using a regular expression match on the key.

Does there exist some kind of collection (key, value) where values can be found using a regular expression match on the key.

Of course I could loop through all the keys and d开发者_如何学Pythono a match, but I was wondering if something smarter is possible.

If not, any ideas on how to acomplish this would be greatly appreciated.

TIA

Søren


You could use a Trie structure, and walk it according to a simple regular expression. It would be difficult to adopt an existing regex library for this purpuse, though.

a -> select child 'a'.
[a-z] -> select all children between 'a' and 'z', inclusive.
. -> select all children.
a* -> select all decendants down 'a' branches.
a? -> select current nodes, and any 'a' children.

When reaching the end of the pattern, return all currently selected nodes. If the number of selected nodes becomes zero, abort and return an empty set.

If using branches, you would have to explore all possible combinations of the pattern.

A good read about efficient regular expressions is Russ Cox articles on the subject.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号