469,640 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

regular expression dictionary key search

50
Wow, I really am full of horrid questions today.

I have a dictionary with a list of patterns:
Expand|Select|Wrap|Line Numbers
  1. >>> words = {'sho.':6, '.ilk':8,'.an.':78 }
Where the "." character means any pattern - this can easily be changed to the "*" symbol if need be.

When the user submits a word, I want to be able to look for a corresponding pattern (if it exists). For example if the user said "show" or "shoe", then the value 6 would be returned. If it was "band", "land", "sand", "pant" etc then 78 would be returned - but not "pants" as it is longer than the pattern.

I know the normal way is to provide the reg exp and search the dictionary with it, but this is the other way round :(

Thanks
Aug 18 '07 #1
4 23659
bartonc
6,596 Expert 4TB
Wow, I really am full of horrid questions today.

I have a dictionary with a list of patterns:
Expand|Select|Wrap|Line Numbers
  1. >>> words = {'sho.':6, '.ilk':8,'.an.':78 }
Where the "." character means any pattern - this can easily be changed to the "*" symbol if need be.

When the user submits a word, I want to be able to look for a corresponding pattern (if it exists). For example if the user said "show" or "shoe", then the value 6 would be returned. If it was "band", "land", "sand", "pant" etc then 78 would be returned - but not "pants" as it is longer than the pattern.

I know the normal way is to provide the reg exp and search the dictionary with it, but this is the other way round :(

Thanks
The '$' is the "end of string" operator:
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> reObj = re.compile('.an.$')
  3. >>> bool(reObj.match("pants"))
  4. False
  5. >>> bool(reObj.match("pant"))
  6. True
  7. >>> 
  8. >>> words = ["show", "shoe", "band", "land", "sand", "pant", "pants"]
  9. >>> pattDict= {'sho.$':6, '.ilk$':8,'.an.$':78 }
  10. >>> for word in words:
  11. ...     for k, v in pattDict.items():
  12. ...         if re.match(k,word):
  13. ...             print word, v
  14. ...             
  15. show 6
  16. shoe 6
  17. band 78
  18. land 78
  19. sand 78
  20. pant 78
  21. >>> 
Hope that helps.
Aug 18 '07 #2
kdt
50
It sickens me that you make it look so easy! This problem has been the bottleneck for two projects. Thank you so much :)
Aug 18 '07 #3
bartonc
6,596 Expert 4TB
It sickens me that you make it look so easy! This problem has been the bottleneck for two projects. Thank you so much :)
<Which part: The k, v thing or the $ thing?>
Cheer up... I actually struggled with the $ thing for a bit.
Aug 19 '07 #4
kdt
50
Thankfully just the $ thing. :)
Aug 19 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Erik Lechak | last post: by
6 posts views Thread by JohnSouth | last post: by
6 posts views Thread by Ludwig | last post: by
Iasthaai
5 posts views Thread by Iasthaai | last post: by
1 post views Thread by NvrBst | last post: by
9 posts views Thread by micron_make | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.