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

Emile van Sebille emile at fenx.com
Sun Jun 23 11:54:59 EDT 2002

"Ben Fairbank" <baf at texas.net> wrote in message
news:3d15e215.43448155 at news.texas.net...
> 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)

>>> s = 'spam and eggs and ham and eggs and spam and eggs'
>>> s.count('am')


Emile van Sebille
emile at fenx.com


More information about the Python-list mailing list