import re


def backends_to_str(target_backends):
  """Creates a flattened and normalized string representing target_backends."""
  normalized_backends = []
  for backend in target_backends:
    # Remove unusual characters and ensure names don't end or start in "_".
    backend = re.sub("[^0-9a-zA-Z_]+", "_", backend)
    normalized_backends.append(backend.strip("_"))
  return "__".join(normalized_backends)