from typing import List
from typing import Tuple
from typing import Collection
import re


def filter_parts(parts: List[Tuple[str,int,int]], selected_parts: Collection[str]) -> List[Tuple[str,int,int]]:
    """
    Filter the given parts with the filter list supplied.
    :param parts: The input list.
    :param selected_parts: A set of strings that may be equal to parts (headers /
                           sections) to keep or may be a regex enclose in
                           slashes ("/regex/").
    :return: The filtered list. If selected parts is empty, everything will be kept.
    """
    if len(selected_parts) == 0:
        return parts
    result: List[Tuple[str,int,int]] = []
    for (name,offset,length) in parts:
        for filter in selected_parts:
            if filter.startswith("/") and filter.endswith("/"):
                if re.match(filter[1:-1], name):
                    result.append((name,offset,length))
            else:
                if filter == name:
                    result.append((name,offset,length))
    return result