def _find_closing_brace(string, start_pos):
 bracks_open = 1
 escaped = False
 for (idx, char) in enumerate(string[start_pos:]):
  if (char == '('):
   if (not escaped):
    bracks_open += 1
  elif (char == ')'):
   if (not escaped):
    bracks_open -= 1
   if (not bracks_open):
    return ((start_pos + idx) + 1)
  if (char == '\\'):
   escaped = (not escaped)
  else:
   escaped = False

def _find_closing_brace ( string , start_pos ) : bracks_open = 1 escaped = False for ( idx , char ) in enumerate ( string [ start_pos : ] ) : if ( char == '(' ) : if ( not escaped ) : bracks_open += 1 elif ( char == ')' ) : if ( not escaped ) : bracks_open -= 1 if ( not bracks_open ) : return ( ( start_pos + idx ) + 1 ) if ( char == '\\' ) : escaped = ( not escaped ) else : escaped = False
finds the corresponding closing brace after start_pos .