from typing import Dict
from typing import List
import re


def get_repo_name_from_url(
    repo_url: str,
    default_version: str,
    valid: Dict[str, List[str]],
) -> str:
    """
    Extract repository name from URL.

    If it cannot be extracted, the name of the first package of the default version is used.

    :param repo_url: the repository URL
    :param default_version: the default version
    :param valid: the dictionary of valid versions & packages
    :return: the repo name, or a default package if it failed
    """
    repo_name = None
    repo_name_match = re.match('.*/(.*)$', repo_url)
    if repo_name_match:
        # Remove '.git' extension if it is there
        repo_name = repo_name_match[1].replace('.git', '')
    else:
        # Default to first package of first version
        repo_name = valid[default_version][0]
    return repo_name