import re


def parse_fasta_header(line):
    """
    Returns gene_name, [(start, end), ..], strand for a given fasta header line.

    >>> parse_fasta_header(">lcl|NC_000913.2_cdsid_NP_417358.2 [gene=xanQ] [protein=xanthine permease] [protein_id=NP_417358.2] [location=3022373..3023773]")
    ('xanQ', [(3022373, 3023773)], '+')
    
    >>> parse_fasta_header(">lcl|NC_000913.2_cdsid_NP_414616.1 [gene=leuA] [protein=2-isopropylmalate synthase] [protein_id=NP_414616.1] [location=complement(81958..83529)]")
    ('leuA', [(81958, 83529)], '-')

    >>> parse_fasta_header(">lcl|NC_000913.2_cdsid_NP_417367.1 [gene=prfB] [protein=peptide chain release factor RF-2] [protein_id=NP_417367.1] [location=complement(join(3033206..3034228,3034230..3034304))]")
    ('prfB', [(3033206, 3034228), (3034230, 3034304)], '-')
    """

    # Regular expressions to match id and location
    #exp_id = re.compile("\[protein_id=([a-zA-Z0-9_\.]+)\]")
    exp_id = re.compile("\[gene=([a-zA-Z0-9]+)\]")
    exp_loc = re.compile("\[location=([a-zA-Z0-9_\.(),]+)\]")

    positions = []
    strand = '+'
    protein_id = None
    m = exp_id.search(line)
    if m:
        protein_id = m.group(1)
    start, end = None, None
    m = exp_loc.search(line)
    if m:
        loc_str = m.group(1)
        if loc_str.startswith("complement"):
            strand = '-'
            loc_str = loc_str[11:-1]
        if loc_str.startswith("join"):
            loc_str = loc_str[5:-1]
            for pair in loc_str.split(","):
                start, end = map(int, pair.split(".."))
                positions.append((start, end))
        else:
            start, end = map(int, loc_str.split(".."))
            positions.append((start, end))
    return protein_id, positions, strand