import re


def _sanitize(name):
    """Removes any characters in string name that aren't alnum or in '_.

    Any spaces will be replaced with underscores. If a name starts with an
    underscore or a period it's removed.

    Only alphanumeric characters, or underscores and periods, are allowed.

    Args:
        name : (str)
            The name to be sanitized.

    Raises:
        N/A

    Returns:
        (str)
            Sanitized name.

    """
    if not name:
        # If not name, it's probably an empty string, but let's throw back
        # exactly what we got.
        return name
    # Replace any spaces with underscores
    name = name.replace(' ', '_')
    # If we start our string with an underscore or period, remove it
    if name[0] in '_.':
        name = name[1:]
    # a-z is all lowercase
    # A-Z is all uppercase
    # 0-9 is all digits
    # \. is an escaped period
    # _ is an underscore
    # Put them together, negate them by leading with an ^
    # and our compiler will mark every non alnum, non ., _ character
    pattern = re.compile(r'[^a-zA-Z0-9\._-]+')
    # Then we sub them with nothing
    fixed = pattern.sub('', name)

    return fixed