import re


def remove_thermo_data(thermo_data1, thermo_data2, group_additivity=False, verbose=False):
    """
    Remove the thermodynamic data `thermo_data2` from the data `thermo_data1`,
    and return `thermo_data1`.
    If `verbose` is True, append ' - thermo_data2.comment' to the thermo_data1.comment.
    If `verbose` is False, remove the thermo_data2.comment from the thermo_data1.comment.
    """
    if (len(thermo_data1.Tdata.value_si) != len(thermo_data2.Tdata.value_si) or
            any([T1 != T2 for T1, T2 in zip(thermo_data1.Tdata.value_si, thermo_data2.Tdata.value_si)])):
        raise ValueError('Cannot take the difference between these ThermoData objects due to their having different '
                         'temperature points.')

    for i in range(thermo_data1.Tdata.value_si.shape[0]):
        thermo_data1.Cpdata.value_si[i] -= thermo_data2.Cpdata.value_si[i]
    thermo_data1.H298.value_si -= thermo_data2.H298.value_si
    thermo_data1.S298.value_si -= thermo_data2.S298.value_si

    if group_additivity:
        if verbose:
            thermo_data1.comment += ' - {0}'.format(thermo_data2.comment)
        else:
            thermo_data1.comment = re.sub(re.escape(' + ' + thermo_data2.comment), '', thermo_data1.comment, 1)
    return thermo_data1