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

Dave Reed dreed at capital.edu
Sun Jun 23 12:06:05 EDT 2002


> From: "Emile van Sebille" <emile at fenx.com>
> 
> "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')
> 3
> >>>

I don't think he wants to count those, only ' am '

There is a much easier solution than what I originally posted:

import re
s = 'the word is the word but not a keyword'
re.findall('\sword\s')

outputs: [' word ', ' word ']
so len(re.findall('\sword\s') will do what he wants

Dave





More information about the Python-list mailing list