import re


def _build_col_name(col_name, multiselect_answer):
    """For a multiselect response, form a stable metadata variable name

    Parameters
    ----------
    col_name : str
        The basename for the column which would correspond to the question.
    multiselect_answer : str
        The selected answer

    Returns
    -------
    str
        The formatted column name, For example, in the primary survey
        there is a multiple select option for alcohol which includes beer
        and wine. The basename would be "alcohol", one multiselect_answer
        would be "beer", and the formatted column name would be
        "alcohol_beer".

    Raises
    ------
    ValueError
        If there are removed characters as it may create an unsafe column name.
        For example, "A+" and "A-" for blood types would both map to "A".
    """
    # replace some characters with _
    multiselect_answer = multiselect_answer.replace(' ', '_')
    multiselect_answer = multiselect_answer.replace('-', '_')

    reduced = re.sub('[^0-9a-zA-Z_]+', '', multiselect_answer)
    return f"{col_name}_{reduced}"