import re


def get_qm_energies(qm_scan_file):

    """
    Returns QM optimized energies from the torsiondrive
    scan file.

    Parameters
    ----------
    qm_scan_file : str
        Output scan file containing torsiondrive scans.

    Returns
    -------
    qm_energies : list
        List of all the qm optimiseed energies extracted from the torsiondrive
        scan file.
    """
    with open(qm_scan_file, "r") as f:
        lines = f.readlines()
    energy_dihedral_lines = []
    for i in range(len(lines)):
        if "Dihedral" in lines[i]:
            energy_dihedral_lines.append(lines[i])
    qm_energies = []
    for i in energy_dihedral_lines:
        energy_dihedral = i
        energy_dihedral = re.findall(r"[-+]?\d+[.]?\d*", energy_dihedral)
        energy = float(energy_dihedral[1])
        qm_energies.append(energy)
    return qm_energies