I've officially taken over the DiceRoller addon from years and years ago and I've reworked it a lot since I've taken it over and done a lot of testing in game. While I haven't uploaded anything yet, I've been struggling on a piece of regex expression that is currently crucial to the design of the addon.
Some background: the newest iteration of the DiceRoller addon makes it so you can type "!XdY" (where X is the number of dice, Y is the dice value) into raid chat and the DM who has the addon will go through some logic in the addon (random number lua protocol) and then spit out an input after adding up the dice.
It is as follows:
local count, size = string.match(message, "^!(%d+)[dD](%d+)$")
Now the functionality I need it to do is parse for both "!XdY" OR "XdY+Z", but it seems as if I can't get close to "XdY+Z" no matter which regex expression I use since I need it to do both expressions. I can provide more source code context if necessary.
Thanks for the help ahead of time, I've always struggled with regex.
Yes; I don't want the exclamation point to be optional, I want the "+Z" to be optional. I just tried your method in-game and it looks like that regex doesn't exactly work: only the !XdY+Z/XdY+Z function using your regex will work, not the !XdY/XdY function.
Maybe it's due to how I'm storing the three variables (X, Y, and Z) and that's causing the issue? If Z is never found and stored, maybe that's causing the issue?
local count, size, modifier = text:match("^!(%d+)[dD](%d+)+?(%d*)$")
count = tonumber(count)
-- There is no match
if not count then return end
size = tonumber(size)
-- The modifier may be nil even matched
modifier = tonumber(modifier) or 0
local ret = 0
for i = 1, tonumber(count) do ret = ret + math.random(size) end
return ret + modifier
print( Parse("!2e10") )
print( Parse("!2d10") )
print( Parse("!2D10+100") )