Search code examples
pythonregexstringstandard-library

Can I search a slice of a string in Python but keep the index relative to the original string?


I have a large string. I regularly have to search only parts of this string, but I do need to now where in the large string the bits found in the slices are found.

Is there a way to use a 'mask' on a string? That is

original = 'This is a mock-up large string'
a_slice = original[10:23]
a_slice.find('o')  
>>> 1 in a_slice; 11 in original

Simply repeating the search is no option as that is too CPU costly.

Update

The toy example above uses find. In practice I use re.finditer().


Solution

  • Like requested, if you want to use finditer (which returns an iterator of Match-objects):

    >>> import re
    >>> original = 'This is a mock-up large string'
    >>> p = re.compile('o')
    >>> for match in p.finditer(original, 10, 23):
    ...  print match.pos
    10
    

    Just a short note: finditer() function (https://docs.python.org/2/library/re.html#re.finditer) is not the same as finditer() method on a regex object (https://docs.python.org/2/library/re.html#re.RegexObject.finditer)