As suggested in the comment by @SruthiV, you can use re.findall from re
module,
import re
pattern = re.compile(r"Hello")
total = 0
with open('text.txt', 'r') as fin:
for line in fin:
total += len(re.findall(pattern, line))
print total
re.compile
creates a pattern for regex to use, here "Hello"
. Using re.compile
improves programs performance and is (by some) recommended for repeated usage of the same pattern. More here.
Remaining part of the program opens the file, reads it line by line, and looks for occurrences of the pattern in every line using re.findall
. Since re.findall
returns a list of matches, total is updated with the length of that list, i.e. number of matches in a given line.
Note: this program will count all occurrences of Hello
- as separate words or as part of other words. Also, it is case sensitive so hello
will not be counted.