def eval_math_expressions(filelist):
    for line in filelist:
        if re.match('.*[\\-|\\+|\\*|\\/].*', line):
            lindex = int(filelist.index(line))
line_list = line.split()
exp = []
for word in line_list:
    if re.match('^\\(+\\d+', word) or re.match('^[\\)+|\\d+|\\-|\\+|\\*|\\/]',
        word):
        exp.append(word)
else:
    ready = ' '.join(exp)
if ready:
    eval_ready = str(eval(ready))
line_new = line.replace(ready, eval_ready)
line = line_new
filelist[lindex] = line
exp = []
return filelist
Python - Evaluate math expression within string