searching dict key with reqex

James Stroud jstroud at mbi.ucla.edu
Tue Aug 21 04:42:09 EDT 2007


james_027 wrote:
> hi,
> 
> can I use regex instead of a plain string with this kind of syntax ...
> 
> 'name' in a_dictionary
> 
> something like
> 
> r'name_\D+' in a_dictionary?
> 
> Thanks
> james
> 

This makes it a one-liner:

import re

def rgxindict(rgx, adict):
   return any(re.match(rgx,k) for k in adict)

James




More information about the Python-list mailing list