import re


def get_changed_flavors(changed_files, flavors):
    """
    Detects changed flavors from a list of changed files.

    Examples
    --------
    >>> flavors = ["pytorch", "xgboost"]
    >>> get_changed_flavors(["mlflow/pytorch/__init__.py", "mlflow/xgboost.py"], flavors)
    ['pytorch', 'xgboost']
    >>> get_changed_flavors(["mlflow/xgboost.py"], flavors)
    ['xgboost']
    >>> get_changed_flavors(["tests/xgboost/test_xxx.py"], flavors)
    ['xgboost']
    >>> get_changed_flavors(["tests/xgboost_autolog/test_xxx.py"], flavors)
    ['xgboost']
    >>> get_changed_flavors(["tests/xgboost_autologging/test_xxx.py"], flavors)
    ['xgboost']
    >>> get_changed_flavors(["README.rst"], flavors)
    []
    >>> get_changed_flavors([], flavors)
    []
    """
    changed_flavors = []
    for f in changed_files:
        pattern = r"^(mlflow|tests)/(.+?)(_autolog(ging)?)?(\.py|/)"
        #                           ~~~~~
        #                           # This group captures a flavor name
        match = re.search(pattern, f)

        if (match is not None) and (match.group(2) in flavors):
            changed_flavors.append(match.group(2))

    return changed_flavors