#NEWLINE# def re_parts( regex_list , text ) : #NEWLINE# #INDENT# "\n    An iterator that returns the entire text, but split by which regex it\n    matched, or none at all.  If it did, the first value of the returned tuple\n    is the index into the regex list, otherwise -1.\n\n    >>> first_re = re.compile('asdf')\n    >>> second_re = re.compile('an')\n    >>> list(re_parts([first_re, second_re], 'This is an asdf test.'))\n    [(-1, 'This is '), (1, 'an'), (-1, ' '), (0, 'asdf'), (-1, ' test.')]\n\n    >>> list(re_parts([first_re, second_re], 'asdfasdfasdf'))\n    [(0, 'asdf'), (0, 'asdf'), (0, 'asdf')]\n\n    >>> list(re_parts([], 'This is an asdf test.'))\n    [(-1, 'This is an asdf test.')]\n\n    >>> third_re = re.compile('sdf')\n    >>> list(re_parts([first_re, second_re, third_re], 'This is an asdf test.'))\n    [(-1, 'This is '), (1, 'an'), (-1, ' '), (0, 'asdf'), (-1, ' test.')]\n    " #NEWLINE# def match_compare( x , y ) : #NEWLINE# #INDENT# return ( x . start ( ) - y . start ( ) ) #NEWLINE# #UNINDENT# prev_end = 0 #NEWLINE# iter_dict = dict ( ( ( r , r . finditer ( text ) ) for r in regex_list ) ) #NEWLINE# matches = [ ] #NEWLINE# for ( regex , iterator ) in iter_dict . items ( ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# match = iterator . next ( ) #NEWLINE# heappush ( matches , ( match . start ( ) , match ) ) #NEWLINE# #UNINDENT# except StopIteration : #NEWLINE# #INDENT# iter_dict . pop ( regex ) #NEWLINE# #UNINDENT# #UNINDENT# while matches : #NEWLINE# #INDENT# ( start , match ) = heappop ( matches ) #NEWLINE# end = match . end ( ) #NEWLINE# if ( start > prev_end ) : #NEWLINE# #INDENT# ( yield ( ( - 1 ) , text [ prev_end : start ] ) ) #NEWLINE# #UNINDENT# ( yield ( regex_list . index ( match . re ) , text [ start : end ] ) ) #NEWLINE# if ( match . re in iter_dict ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# newmatch = iter_dict [ match . re ] . next ( ) #NEWLINE# heappush ( matches , ( newmatch . start ( ) , newmatch ) ) #NEWLINE# #UNINDENT# except StopIteration : #NEWLINE# #INDENT# iter_dict . pop ( match . re ) #NEWLINE# #UNINDENT# #UNINDENT# prev_end = end #NEWLINE# #UNINDENT# last_bit = text [ prev_end : ] #NEWLINE# if ( len ( last_bit ) > 0 ) : #NEWLINE# #INDENT# ( yield ( ( - 1 ) , last_bit ) )