import re


def parse_cuda_device_id(device: str) -> int:
    """Parse cuda device index from a string.

    Args:
        device (str): The typical style of string specifying cuda device,
            e.g.: 'cuda:0'.

    Returns:
        int: The parsed device id, defaults to `0`.
    """
    match_result = re.match('([^:]+)(:[0-9]+)?$', device)
    assert match_result is not None, f'Can not parse device {device}.'
    assert match_result.group(1).lower() == 'cuda', 'Not cuda device.'

    device_id = 0 if match_result.lastindex == 1 else int(
        match_result.group(2)[1:])

    return device_id