import re


def ParseElastix(input,par):
    """Parse an elastix parameter file or elastix.log file and
    extract a number associated with the given string parameter.

    Parameters
    ----------
    input: string
        Path to input file.

    par: string
        String indicating the parameter in the files to extract.

    Returns
    -------
    number: string
        Orginal string found in the given file. This will be converted to an
        integer or a floating point number.

    num: integer or float
        Number corresponding to the parameter specified.
    """

    #Read the transform parameters
    with open(input, 'r') as file:
        filedata = file.readlines()
    #Add each line to a list with separation
    result=[]
    for x in filedata:
        result.append(x.split('\n')[0])
    #Find the parameter (Add a space for a match)
    lines = [s for s in result if str(par+' ') in s][-1]
    number = re.findall(r"[-+]?\d*\.\d+|\d+", lines)[0]
    #Try to convert to integer, otherwise convert to float
    if number.isdigit():
        num = int(number)
    else:
        num = float(number)
    #Return the number
    return number, num