code: def bounds(filename, start_re, end_re, encoding='utf8'):
      
      start_re, end_re = re.compile(start_re), re.compile(end_re)
      mo, line_start, line_end, byte_start, byte_end = [None]*5
      offset = 0
  
      with open(filename, 'rb') as f:
          for index, line in enumerate(f):
              line_text = line.decode(encoding)
              start_match = re.match(start_re, line_text)
              if start_match:
                  mo, line_start, byte_start = start_match, index, offset
  
              offset += len(line)
  
              end_match = re.match(end_re, line_text)
              if end_match:
                  yield mo, Bounds(line_start, index, byte_start, offset)
                  mo, line_start, line_end, byte_start, byte_end = [None]*5
docs: Compute chunk bounds from text file according to start_re and end_re:
      yields (start_match, Bounds) tuples.