regular expression dictionary key search

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdt
    New Member
    • Mar 2007
    • 50

    regular expression dictionary key search

    Wow, I really am full of horrid questions today.

    I have a dictionary with a list of patterns:
    Code:
    >>> 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
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by kdt
    Wow, I really am full of horrid questions today.

    I have a dictionary with a list of patterns:
    Code:
    >>> 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:[CODE=python]
    >>> import re
    >>> reObj = re.compile('.an .$')
    >>> bool(reObj.matc h("pants"))
    False
    >>> bool(reObj.matc h("pant"))
    True
    >>>
    >>> words = ["show", "shoe", "band", "land", "sand", "pant", "pants"]
    >>> pattDict= {'sho.$':6, '.ilk$':8,'.an. $':78 }
    >>> for word in words:
    ... for k, v in pattDict.items( ):
    ... if re.match(k,word ):
    ... print word, v
    ...
    show 6
    shoe 6
    band 78
    land 78
    sand 78
    pant 78
    >>> [/CODE]Hope that helps.

    Comment

    • kdt
      New Member
      • Mar 2007
      • 50

      #3
      It sickens me that you make it look so easy! This problem has been the bottleneck for two projects. Thank you so much :)

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by kdt
        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.

        Comment

        • kdt
          New Member
          • Mar 2007
          • 50

          #5
          Thankfully just the $ thing. :)

          Comment

          Working...