How to find all occurrences of a substring?

There is no simple built-in string function that does what you’re looking for, but you could use the more powerful regular expressions:

import re

[m.start() for m in re.finditer(‘test’, ‘test test test test’)]

#[0, 5, 10, 15]

If you want to find overlapping matches, lookahead will do that:

[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]

If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:

search = 'tt'

[m.start() for m in re.finditer(‘(?=%s)(?!.{1,%d}%s)’ % (search, len(search)-1, search), ‘ttt’)]

#[1]

re.finditer returns a generator, so you could change the [] in the above to () to get a generator instead of a list which will be more efficient if you’re only iterating through the results once.

Leave a Comment