python re module for salvation.
>>> import re >>> [x.start() for x in re.finditer('foo', 'foo foo foo foo')] [0, 4, 8, 12]
re.finditer returns a generator , which means that instead of using list-comprehensions, you can use in in a for-loop , which will be more memory efficient.
You can expand this to get the range of your template in a given text. that is, the index of the beginning and end.
>>> [x.span() for x in re.finditer('foo', 'foo foo foo foo')] [(0, 3), (4, 7), (8, 11), (12, 15)]
Not Python Awesome :) couldn't stop myself from quoting XKCD, downvotes or without downvotes ...

source share