import re


def parse_contig_file_name(ref_names, taxon_names, contig_file):
    """Extract the reference & taxon names from the contig file name."""
    sep = r'[_. ]'

    ref_names = [(x, re.sub(sep, sep, x) + sep) for x in ref_names]
    ref_names = sorted(
            ref_names, key=lambda x: (len(x[1]), x), reverse=True)

    taxon_names = [(x, re.sub(sep, sep, x) + sep) for x in taxon_names]
    taxon_names = sorted(
            taxon_names, key=lambda x: (len(x[1]), x), reverse=True)

    ref_name = [x[0] for x in ref_names if re.search(x[1], contig_file)]
    taxon_name = [x[0] for x in taxon_names if re.search(x[1], contig_file)]

    ref_name += [None]
    taxon_name += [None]

    return ref_name[0], taxon_name[0]