import re


def categorize_tags(tagset):
    """
    Categorize the tags in `tagset` into: domains, levels, disregard, target, background, plus.

    Parameters
    ----------
    tagset: list
        list of dicts; each dict has a 'tag_name' key

    Returns
    -------
    dict
        dict of categories and the tags belonging to each of them
    """
    tag_names = [i['tag_name'] for i in tagset]
    # define regexes
    rdomain = re.compile('\..*')
    rlevel = re.compile('[a-z]{3}\d')
    rdisregard = re.compile('other_disregard_file')
    rtarget = re.compile('other_target')
    rbackground = re.compile('other_background')
    rplus = re.compile('mbw_plus')
    # find categories
    domains = [tag for tag in tag_names if rdomain.match(tag)]
    levels = [tag for tag in tag_names if rlevel.match(tag)]
    disregard = [tag for tag in tag_names if rdisregard.match(tag)]
    target = [tag for tag in tag_names if rtarget.match(tag)]
    background = [tag for tag in tag_names if rbackground.match(tag)]
    plus = [tag for tag in tag_names if rplus.match(tag)]
    return dict(
        domains=domains,
        levels=levels,
        disregard=disregard,
        target=target,
        background=background,
        plus=plus,
    )