import re


def filter_collection(collection, include=None, exclude=None):
    """Filter collection based on include and exclude regexps.

    The regexp patterns are not implicitly anchored at the beginning of
    strings (i.e. `search` is used, rather than `match`). E.g.
    'abc' will match '_abc_' as well as 'abcdef'.

    :return: set of matching collection elements
    """
    matches = set()
    if include:
        include_pat = re.compile(include)
    else:
        include_pat = None
    if exclude:
        exclude_pat = re.compile(exclude)
    else:
        exclude_pat = None

    for element in collection:
        if include_pat and not re.search(include_pat, element):
            continue  # Do not include this database; failed to match include
        if exclude_pat and re.search(exclude_pat, element):
            continue  # Do not include this database; matched exclude
        matches.add(element)

    return matches