473,581 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ 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 27319
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
7280
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 does this? Is there a way and would it be better to use list comprehension? (using python 2.3) Just looking for a better or more pythonic way to do...
1
4158
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) ...
9
3144
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 strkey = "something"; var str = "Somethin like this"; if( str.search( / + strkey + / ) > -1 )
6
489
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 with a "&" character in the local part. I know I should be able to work it out myself, but I'd like to ask anyone to suggest the best way to
6
2287
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 work: it seems that < is not a valid character, so the beginning of the word starts at theletter 'p' instead of '<'. Because I'm not an expert in...
5
1526
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 before I store words in a dictionary. I basically want to read in the words and remove any numbers, punctuation, or other special characters from the...
3
2199
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 PrmID def OnTextEntered(self, evt): text = self.GetValue() if re.search('\', text): text.replace(' ...
1
3386
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 "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though:...
9
2477
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
7882
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5366
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.