regular expressions as hash keys in python

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

    regular expressions as hash keys in python

    hi, bit of a strange question but does anyone know if there is a way of storing regular expressions as dictionary keys in python?

    I ask because i need to match patterns of sequences against other sequences and the patterns themselves are the most important thing. If I could store the patterns in a dictionary, and then cycle through them to see if the patterns are in another sequence, this would be ideal.

    thanks
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by kdt
    hi, bit of a strange question but does anyone know if there is a way of storing regular expressions as dictionary keys in python?

    I ask because i need to match patterns of sequences against other sequences and the patterns themselves are the most important thing. If I could store the patterns in a dictionary, and then cycle through them to see if the patterns are in another sequence, this would be ideal.

    thanks
    you can put them values
    eg
    Code:
    >>> d = { "1": "\d+" , '2': "\w+" }
    >>> data = open("file").read()
    >>> for i, j in d.iteritems():
    ...  print re.findall(i,data)
    ...
    ['1']
    ['2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2']

    Comment

    Working...