import re
import uuid


def extract_uuid(filename):
    """
    Given a filename containing a kernel, extract the uuid in
    the kernel name.

    Parameters
    ----------
    filename : str
        The name of the kernel-...-json file with the connection info.

    Returns
    -------
    uuid : str or None
        The extracted uuid, or None if not found.
    """

    extracted = re.match(
        ".*kernel-([0-9a-f]{8}-?[0-9a-f]{4}-?4[0-9a-f]{3}-"
        "?[89ab][0-9a-f]{3}-?[0-9a-f]{12}).json",
        filename,
    )
    if extracted is not None:
        return uuid.UUID(extracted.group(1))
    else:
        return None