python - Getting exact string from a txt file -


let's trying number of times "the" appears in text file. how word "the" alone :

without getting others words 'the' substring :

  • "there",
  • "them",
  • "their", etc.

word = 'the' reg = re.compile(r'\b%s\b' % word, re.i) open('textfile.containing.the','r') fin:      print(len(reg.findall(fin.read())) 

hope helps..


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -