import re


def create_metadata_filename_regex(metadata, study_id):
    """Create regular expressions for study metadata files.

    Parameters
    ----------
    metadata : str, required
        Type of metadata file ('sample' or 'prep'), found in the study_path,
        from which to extract attributes.
    study_id : str, required
        The identifier of the study to which regular expressions should apply.

    Returns
    -------
    re.Pattern
        Compiled regular expression allowing search for a particular study's
        metadata files.
    """
    if metadata == 'sample':
        regex = re.compile(r'^{}_(?!prep).*'.format(study_id))
    elif metadata == 'prep':
        regex = re.compile(r'^{}_prep_.*?_qiime.*'.format(study_id))
    else:
        raise ValueError('Unknown metadata type. Accepted options are "sample" '
                         'or "prep".')
    return regex