Python has string.find()
and string.rfind()
to get the index of a substring in string.
I wonder, maybe there is something like string.find_all()
which can return all founded indexes (not only first from beginning or first from end)?
For example:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#that's the goal
print string.find_all('test') # [0,5,10,15]
this is an old thread but i got interested and wanted to share my solution.
It should return a list of positions where the substring was found. Please comment if you see an error or room for improvment.
Come, let us recurse together.
No need for regular expressions this way.
Whatever the solutions provided by others are completely based on the available method find() or any available methods.
Calling the method
The pythonic way would be:
Again, old thread, but here's my solution using a generator and plain
str.find
.Example
returns
When looking for a large amount of key words in a document, use flashtext
Flashtext runs faster than regex on large list of search words.