Hashtable/dictionary/map lookup with regular expressions

前端 未结 19 1530
难免孤独
难免孤独 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条回答
  •  萌比男神i
    2021-02-01 06:17

    There is a Perl module that does just this Tie::Hash::Regex.

    use Tie::Hash::Regex;
    my %h;
    
    tie %h, 'Tie::Hash::Regex';
    
    $h{key}   = 'value';
    $h{key2}  = 'another value';
    $h{stuff} = 'something else';
    
    print $h{key};  # prints 'value'
    print $h{2};    # prints 'another value'
    print $h{'^s'}; # prints 'something else'
    
    print tied(%h)->FETCH(k); # prints 'value' and 'another value'
    
    delete $h{k};   # deletes $h{key} and $h{key2};
    

提交回复
热议问题