(Newbie) Counting Instances ("Hits") with Regular Expressions

Ben Fairbank baf at texas.net
Sun Jun 23 17:08:17 CEST 2002

I am new both to Python and to regular expressions, which may account
for my difficulty.  I must count the frequenies of certain words in
files of moderate length (about150 k bytes).  I have been reading
files and then using count(s,sub), which is fast and easy.  I now have
to allow for punctuation and eliminate words within words, etc, and so
am trying to use regular expressions instead of simple words as
targets.  I do not, however, find a similarly easy to use count
function in the re module.  Yet this is such common operation it must
be there, or easy to implement.  What is the usual way of simply
counting "hits" in the re module?  (And what have I missed in the
documentation; where is this to be found?  I have looked through Lutz
and Ascher)

Thanks for any help.


More information about the Python-list mailing list