import re


def subject_id_of(path) -> str:
    """
    Returns the subject ID closest to the end of the input string or Path.

    Inputs
    ------
    path : str or Path
        String or Path containing the subject ID.

    Returns
    -------
    str
        Subject ID found in the filename

    Raises
    ------
    RuntimeError
        If no subject ID found in input filename.
    """

    try:
        subject_ids = re.findall(r"sub-(\d+)", str(path))
        return subject_ids[-1]
    except IndexError:
        raise RuntimeError(f"No subject ID found in {path}")