import re
import random


def get_patches(path, n=0):
    """Get selected number of patch IDs from given directory path.
    If number is not provided, i.e. is zero, all patch IDs are returned.

    :param path: Directory path where patches are
    :type path: Path
    :param n: Number of patch IDs to retrieve, defaults to 0
    :type n: int, optional
    :return: List of patch IDs
    :rtype: list[int]
    """
    patches = [patch.name for patch in path.glob('eopatch_*')]
    ids = []

    for patch in patches:
        match = re.match(r'^eopatch_(\d+)$', patch)
        if match:
            ids.append(int(match.group(1)))

    ids.sort()
    return random.sample(ids, n) if n else ids