import re


def split_hostname_index(name, default=None):
    """
    Split the index out of a hostname.

    E.g. splits "test^1" into ("test", 1). If no index is present, returns
    (name, default).
    """
    match = re.match(r'^(.*)~(\d+)$', name)
    if match:
        return match.group(1), int(match.group(2))
    else:
        return name, default