Newbie: searching an English dictionary for a reg exp

^^@++ ballsacks at xtra.co.enzed
Sun Dec 2 04:31:48 EST 2001


I want to search through the entire English language for a given
regexp.

So, I'll have say a 'dictionary.txt' file which contains every English
word.

Since I don't understand how python regexps work I'm kinda stuck.

My guess is I should be doing something like:
#####################################
import re
import string

# heres our regpattern - once I get it going I'll read 
# regpatterns from a file
regpattern = 'abas?' # should return 'abash' and 'abase'

dictionary = []
dictfile = open('dictionary.txt', 'r')
line = dictfile.readline()
while line != "":
    dictionary.append(line)
    line = string.strip(dictfile.readline())

reg = re.compile(regpattern)

# search thru dictionary - print all matches
for word in dictionary:
    m = reg.match(regpattern)
    if m:
        print m.group() 

#####################################
This doesn't word properly - so how do I print each word that matches?

Have I gone about this in the correct way? Having the entire
dictionary in memory could be dodgy I guess...

Thanks
-Matt



More information about the Python-list mailing list