How to search for multiple conditions in python regex? -
How to search for multiple conditions in python regex? -
i'm writing helper script dnd needs, part of script should take either integers or dice notation. latter in format of
1d20+4
where can replace 1
, 20
, natural number, , +4
integer. input can
hit +3 harm 1d8+1 hp 3d6+2
and split 2 list using re
. problem when seek observe both dies , numbers. know that observe number need use
re.listall('[\+\-][0-9]*',input_line)
and observe die need search for
re.listall('[0-9]*d[0-9]*',input_line)
and i'm quite search die bonus need
re.listall('[0-9]*d[0-9]*[\+\-][0-9]*',input_line)
however, can't figure how search combination of two. though placing them in parenthesis, i.e.
re.listall('([\+\-][0-9]*)([0-9]*d[0-9]*)',input_line)
however, error sre_constants.error: unbalanced parenthesis
which leaves me confused. how can overcome this?
i think need regular expression:
re.findall('((\d+d\d+)?[\+-]\d+)', input_line)
as side note, can utilize \d
instead of [0-9]
. so, sec part same in code. first part '(\d+d\d+)?'
optional (because of ?
) , matches number followed letter d followed number.
in illustration (hit +3 harm 1d8+1 hp 3d6+2
), match +3
, 1d8+1
, 3d6+2
python regex
Comments
Post a Comment