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

Emile van Sebille emile at fcfw.fenx.com
Sun Jun 23 18:17:28 CEST 2002


Dave Reed
> 
> 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
> 

>>> s = 'the word is the word, but not a keyword'
>>> re.findall(r'\b(word)\b', s.lower())
['word', 'word']
>>>

--

Emile van Sebille
emile at fenx.com

---------






More information about the Python-list mailing list