import re


def get_valid_file_ending(s):
    """
    Modified after:
    https://stackoverflow.com/questions/295135/turn-a-string-into-a-valid-filename

    def get_valid_filename(s):
        s = str(s).strip().replace(' ', '_')
        return re.sub(r'(?u)[^-\w.]', '', s)

    In addition, start and end of file ending should start with word or
    number.

    >>> s = "___.hallole123.so_hallole123.___"
    >>> get_valid_file_ending(s)
    'hallole123.so_hallole123'
    >>> get_valid_file_ending("john's new arctic warfare")
    'johns_new_arctic_warfare'

    """
    assert s, "given s empty"
    # Strip and replace spaces with _.
    s = str(s).strip().replace(' ', '_')
    # Remove non-word characters from start and end.
    m = re.search('\W*([a-zA-Z0-9].+[a-zA-Z0-9])\W*', s)
    if m:
        return re.sub(r'(?u)[^-\w.]', '', m.group(1))
    else:
        return re.sub(r'(?u)[^-\w.]', '', s)