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.
精彩评论