import random

import re

 

def rollMulti(rollStr):

    patt = re.compile(r'\d+d\d+', re.IGNORECASE)

    rolls = rollStr.split(',')

    # validate each item in rolls with re expression

    rolls = [roll.strip() for roll in rolls if patt.match(roll.strip())]

    # split on 'd' or 'D'

    rollList = [(int(i), int(j)) for i,j in [re.split('[dD]',item) for item in rolls]]

    outList = []

    for roll in rollList:

        outList.append([random.randint(1, roll[1]) for i in range(roll[0])])

    #return '\n'.join(['* On a roll of %s, you rolled %s. Total: %d' % \

    #                  (rolls[i], outList[i], sum(outList[i])) for i in range(len(rolls))])

 

    # OP wants: You rolled 7,12,11 using 3d4,2d8,4d6 ((3,2,2),(8,4),(1,3,6,1))

    return 'You rolled %s using %s %s' % \

           (','.join([str(sum(lst)) for lst in outList]),\

            ','.join(rolls), \

            tuple([tuple(item) for item in outList]))

 

 

rollStr = '4d6, 5D6,8H7,4d5,9d9'

print rollMulti(rollStr)

 

'''

>>> * On a roll of 4d6, you rolled [5, 5, 4, 2]. Total: 16

* On a roll of 5D6, you rolled [5, 3, 4, 1, 1]. Total: 14

* On a roll of 4d5, you rolled [2, 5, 1, 1]. Total: 9

* On a roll of 9d9, you rolled [5, 9, 4, 4, 6, 8, 3, 5, 5]. Total: 49

>>>

'''