By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

regular expression dictionary key search

P: 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
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 50
kdt
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
Expert 5K+
P: 6,596
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

P: 50
kdt
Thankfully just the $ thing. :)
Aug 19 '07 #5

Post your reply

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