def get_best_match(target_name, names):
 exact = []
 wildcard_start = []
 wildcard_end = []
 regex = []
 for name in names:
  if _exact_match(target_name, name):
   exact.append(name)
  elif _wildcard_match(target_name, name, True):
   wildcard_start.append(name)
  elif _wildcard_match(target_name, name, False):
   wildcard_end.append(name)
  elif _regex_match(target_name, name):
   regex.append(name)
 if (len(exact) > 0):
  match = min(exact, key=len)
  return ('exact', match)
 if (len(wildcard_start) > 0):
  match = max(wildcard_start, key=len)
  return ('wildcard_start', match)
 if (len(wildcard_end) > 0):
  match = max(wildcard_end, key=len)
  return ('wildcard_end', match)
 if (len(regex) > 0):
  match = regex[0]
  return ('regex', match)
 return (None, None)

def get_best_match ( target_name , names ) : exact = [ ] wildcard_start = [ ] wildcard_end = [ ] regex = [ ] for name in names : if _exact_match ( target_name , name ) : exact . append ( name ) elif _wildcard_match ( target_name , name , True ) : wildcard_start . append ( name ) elif _wildcard_match ( target_name , name , False ) : wildcard_end . append ( name ) elif _regex_match ( target_name , name ) : regex . append ( name ) if ( len ( exact ) > 0 ) : match = min ( exact , key = len ) return ( 'exact' , match ) if ( len ( wildcard_start ) > 0 ) : match = max ( wildcard_start , key = len ) return ( 'wildcard_start' , match ) if ( len ( wildcard_end ) > 0 ) : match = max ( wildcard_end , key = len ) return ( 'wildcard_end' , match ) if ( len ( regex ) > 0 ) : match = regex [ 0 ] return ( 'regex' , match ) return ( None , None )
finds the best match for target_name out of names using the nginx name-matching rules .