
import re
import random

regex = r"(?m)^[^\/#]*?([.+-]*\d+(?:\.\d+)?(?:e[+-]?\d+)?) +([^\n]*)"

test_str = """

0.3 играть
0.3 кодить
0.2 спать
0.01 сделать уборку
0.5 разминка шеи

-0.7 временно отключённый пункт
Комментарии свободно где угодно можно писать
Даже тут 0.4 гулять



"""

matches = re.finditer(regex, test_str)
r=[]
s=[]



for matchNum, match in enumerate(matches, start=1):
    class MyException(Exception):
        ...
    N=len(match.groups())
    if (N==2):
        try:
            ww=float(match.group(1))
            if (ww<=0):
                raise MyException()
            r+=[ww]
            s+=[match.group(2)]
            print(f'{r[-1]} : {s[-1]}')            
        except MyException as e:
            print(f"    - {match.group(2)}")
        except Exception as e:
            print(f'Err : {e} : {match.group()}')

N=sum(r)
print('\n')
def roll():
    if (N==0):
        print("Пусто")
        return
    x=random.random()*N
    y=x
    i=0
    while (x>r[i]):
        x-=r[i]
        i+=1
    print(f'{y:.4f}/{N:.4f} : {x:.4f}/{r[i]:.4f} : {s[i]}')


roll()

#print(g)
                
            #print(f'{i} :: {match.group(i+1)}')
    #for groupNum in range(0, len(match.groups())):
   #    groupNum = groupNum + 1
        
     #   print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
