import re


def extract_id(url):
    """Extract the tournament id of the tournament from its name or URL."""
    match = re.search(r'(\w+)?\.?challonge.com/([^/]+)', url)

    if match is None or match.group(2) is None:
        raise ValueError(f'Invalid Challonge URL: {url}')

    subdomain, tourney = match.groups()

    if subdomain is None:
        return tourney
    return f'{subdomain}-{tourney}'