473,473 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

regular expression dictionary key search

50 New Member
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 27248
bartonc
6,596 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
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 New Member
Thankfully just the $ thing. :)
Aug 19 '07 #5

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

Similar topics

3
by: Erik Lechak | last post by:
Hello all, I wrote the code below. It is simply a dictionary that uses regular expressions to match keys. A quick look at _test() will give you an example. Is there a module that already...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
9
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var...
6
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
6
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
5
Iasthaai
by: Iasthaai | last post by:
Hello everyone, I'm new to Python and also new to this forum and I have a question regarding regular expressions, so here goes: I am attempting to read in a text and do some pattern matching...
3
jlm699
by: jlm699 | last post by:
I have an application that is build on wxPython and have run into a small but annoying problem. I use a Search Control in my toolbar just as in the ToolBar example. Now when the user enters say...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
9
by: micron_make | last post by:
I am trying to parse a file whose contents are : parameter=current max=5A min=2A for a single line I used for line in file: print re.search("parameter\s*=\s*(.*)",line).groups()
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.