import re
import random

def xdy(matchobj):
    x, y = map(int, matchobj.groups())
s = 0
for each in range(x):
    s += random.randint(1, y)
return str(s)
s = '1d6+2d6+2d6-72+4d100'
t = re.sub('(\\d+)d(\\d+)', xdy, s)
print(t)
print(eval(t))
How to evaluate a custom math expression in Python