import re


def extract_version(doc):
    """
    Extracts the version from the docstring of an Example.
    The Version is specified in the following format:
    LabOne Version >= <major>.<minor>[.<minor>]

    Returns:
        (major, minor, build)

    Raises:
        Exception if the Version has the wrong format.
        Exception if there is no or more than one LabOne Version specified.
    """
    results = re.findall(r"LabOne Version >= ([0-9\.]*)", doc)
    if len(results) == 0:
        raise Exception("No LabOne Version is defined in the docstring")
    if len(results) > 1:
        raise Exception(
            "more than one LabOne version is defined but only one is allowed"
        )

    version = results[0]
    major_minor_format = bool(re.match(r"^\d\d\.\d\d$", version))
    major_minor_build_format = bool(re.match(r"^\d\d\.\d\d.\d+$", version))

    if major_minor_format:
        min_major, min_minor = map(int, version.split("."))
        min_build = 0
    elif major_minor_build_format:
        min_major, min_minor, min_build = map(int, version.split("."))
    else:
        raise Exception(
            f"Wrong ziPython version format: {version}. Supported format: MAJOR.MINOR or \
                MAJOR.MINOR.BUILD"
        )

    return (min_major, min_minor, min_build)