I want to create a dictionary in which keys are regular expressions:
d = {'a.*': some_value1, 'b.*': some_value2}
Then, when I look in the dictionary:
d['apple']
I want apple 'apple' match keys that are regular expressions. If there is a complete match with the key / regular expression, then the corresponding value should be returned.
For example, 'apple' matches the regular expression 'a.*' some_value1 , so some_value1 returned.
Of course, all this assumes that the regex keys do not conflict (i.e. the two keys should not match exactly the same). Say I can take care of this manually when creating my keys.
Is this possible in Python? If so, it will be a pretty elegant and powerful design!
python dictionary regex
applecider
source share