import re


def select_nights(allnights, nightstr):
    """
    Select nights based on regex matches.

    Given a list of nights, select all nights matching the specified
    patterns and return this subset.

    Args:
        allnights (list): list of all nights as strings
        nightstr (str): comma-separated list of regex patterns.

    Returns:
        list: list of nights that match the patterns.
    """

    nights = []
    if nightstr is not None:
        nightsel = nightstr.split(",")
        for sel in nightsel:
            pat = re.compile(sel)
            for nt in allnights:
                mat = pat.match(nt)
                if mat is not None:
                    if nt not in nights:
                        nights.append(nt)
        nights = sorted(nights)
    else:
        nights = sorted(allnights)

    return nights