def re_rsearch(pattern, text, chunk_size=1024):
 def _chunk_iter():
  offset = len(text)
  while True:
   offset -= (chunk_size * 1024)
   if (offset <= 0):
    break
   (yield (text[offset:], offset))
  (yield (text, 0))
 pattern = (re.compile(pattern) if isinstance(pattern, basestring) else pattern)
 for (chunk, offset) in _chunk_iter():
  matches = [match for match in pattern.finditer(chunk)]
  if matches:
   return ((offset + matches[(-1)].span()[0]), (offset + matches[(-1)].span()[1]))
 return None

def re_rsearch ( pattern , text , chunk_size = 1024 ) : def _chunk_iter ( ) : offset = len ( text ) while True : offset -= ( chunk_size * 1024 ) if ( offset <= 0 ) : break ( yield ( text [ offset : ] , offset ) ) ( yield ( text , 0 ) ) pattern = ( re . compile ( pattern ) if isinstance ( pattern , basestring ) else pattern ) for ( chunk , offset ) in _chunk_iter ( ) : matches = [ match for match in pattern . finditer ( chunk ) ] if matches : return ( ( offset + matches [ ( - 1 ) ] . span ( ) [ 0 ] ) , ( offset + matches [ ( - 1 ) ] . span ( ) [ 1 ] ) ) return None
this function does a reverse search in a text using a regular expression given in the attribute pattern .