Just like the question title.
I’m new to Python and regular expressions. Hereby I have to search for a specific word from a paragraph and show all indices of occurrence.
For example:
the paragraph is:
This is a testing text and used to test and test and test.
and the word:
test
The algorithm should return the index of non-overlapping occurences of 3 words test in the above paragraph (but not testing, because I mean search the whole word, not just substring).
Another example with the same paragraph and this “word”:
test and
The algorithm should return 2 occurrences of test and.
I guess I must use some regular expressions to find the pattern of that whole word, with preceding and following are punctuations such as . , ; ? -
After Googling I found something like re.finditer should be used but it seems that I haven’t found out the right way to go. Please help, thank you in advance. 😉
Yes,
finditeris the way to go. Usestart()to find the index of the match.Example:
Output: