import re


def archive_groups(inp):
    """Create a list of related Let's Encrypt archive-like certificate files.

    For a given input that is a list of archive-like certificate files
    (e.g. 'cert1.pem', 'cert2.pem' etc.), return a list of lists, where the
    inner lists are those files grouped by the number in the file name.

    For example, for an input list:
        [ cert1.pem, chain1.pem, cert2.pem, chain2.pem, cert3.pem ],
    return:
        [ [cert1.pem, chain1.pem], [cert2.pem, chain2.pem], [cert3.pem] ].

    If the input is a list of live-like files (e.g. 'cert.pem', 'chain.pem'
    etc.), where there is no number in the file name, then return an empty
    list.

    Args:
        inp (list(pathlib.Path)): list of absolute paths of files in a
            common directory (usually a Let's Encrypt live or archive folder,
            but can be any folder).

    Returns:
        list(list(pathlib.Path)): list of lists if the input, grouped by the
            number in the file name.
    """
    nums = []
    for c in inp:
        m = re.match(r'\w+(\d+)\.pem$', c.name)
        if m:
            if m.group(1) not in nums:
                nums += [ m.group(1) ]
    return [ [ c for c in inp if re.match(r'\w+{}\.pem$'.format(n), c.name) ]
             for n in nums ]