Newbie: searching an English dictionary for a reg exp

Eugene importbinascii
Tue Dec 4 05:57:58 CET 2001


Oh, sorry about that.  "re.search" only requires that the pattern match at
the start of the target string; it does not also require that the pattern
match all the way to the end.  Put "$" at the end of your pattern and that
will fix it.  (The regular expression "$" means end-of-string.*)

And if you just want to print the word that matched, just put
"outfile.write( word + ' ' )" in place of what you have in your "if" block.
Once you know the word matches, that's good enough for you, right? :)

    -Eugene

* in Python's regular expression and in PERL's.  I have no idea what "$"
means in other languages' regular expression engines.

"^^@++" <ballsacks at xtra.co.enzed> wrote in message
news:3c0b1bbe.173865154 at news.akl.ihug.co.nz...
> On Sun, 2 Dec 2001 13:54:24 -0800, "Eugene" <import binascii; print
> binascii.a2b_base64('ZXVnZW5lQGlzb21lZGlhLmNvbQ==')> wrote:
>
> Oops, looks like I spoke too soon with my last post.
> The matches aren't working properly, in that it seems to be finding
> too many incorrect matches:
>
> def searchfor(regpattern):
>     print "Searching for",regpattern,
>     matchcount = 0
>     reg = re.compile(regpattern, re.I)
>     m = ""
>     for word in dictionary:
>         m = reg.match(word)
>         if m:
>             outfile.write(m.group()+' ')
>             matchcount += 1
>
>     print "Found",matchcount
>     return matchcount
>
> Produces:
> Read 1187 words from the dictionary
> Read 1 clues from clues.txt
> Searching for abac. Found 14
>
> Now, in my dictionary there are only 2 words that would match that
> criteria - abaca and aback, right? It seems to be returning what is I
> guess (abac)*:
> dictionary =
> ...
> Ababua
> abac
> abaca
> abacate
> abacay
> abacinate
> abacination
> abaciscus
> abacist
> aback
> abactinal
> abactinally
> abaction
> abactor
> abaculus
> abacus
> Abadite
> ...
>
> I get the feeling I'm barking up the wrong tree by using m.group().
> All I want is the word that matches! IMO the doco for MatchObject
> (match-objects.html) is useless (or maybe I am).
>
> Thanks for any help
>
> -Matt





More information about the Python-list mailing list