def _get_key_and_indices(maybe_key_with_indices):
  """Extracts key and indices from key in format 'key_name[index0][index1]'."""
  patterns = maybe_key_with_indices.split('[')
  if len(patterns) == 1:
    return (maybe_key_with_indices, None)
  # For each index ensure that the brackets are closed and extract number
  indices = []
  for split_pattern in patterns[1:]:
    # Remove surrounding whitespace.
    split_pattern = split_pattern.strip()
    if split_pattern[-1] != ']':
      raise ValueError(
          'ParameterName {} has bad format. Supported format: key_name, '
          'key_name[index0], key_name[index0][index1], ...'.format(
              maybe_key_with_indices))
    try:
      indices.append(int(split_pattern[:-1]))
    except ValueError:
      raise ValueError(
          'Only integer indexing allowed for ParameterName. '
          'Faulty specification: {}'.format(maybe_key_with_indices))
  return patterns[0], indices