PDA

View Full Version : Lua patterns question



Bidmaron
January 1st, 2017, 04:00
I am starting to come to grips with lua patterns, but I don't understand the greedy and non-greedy behaviors very well. I am trying to see if a string contains at least one die specification. So, there can be an arbitrary number of characters before and then a 'd' and one or more digits followed by an arbitrary number of characters. I am tempted to use the following:

"^[^d]+d%d[^d]+" but that won't handle a string that contains a 'd' without being a die (e.g. 'advanced d6' wouldn't match the pattern).

Also, I suppose it needs to include fudge dice as well, and I'm not sure how to add that in.

Bidmaron
January 1st, 2017, 04:43
Actually, I think the following will work:


if testStr:find("d[d%]+")~=nil then --we have at least one die in the string
....
else --there is no die in the string
....
endif

Ikael
January 1st, 2017, 10:00
Actually, I think the following will work:


if testStr:find("d[d%]+")~=nil then --we have at least one die in the string
....
else --there is no die in the string
....
endif

I believe there is bug, maybe "%d*[dD]%d+"

Would match to
d6
D12
Advanced 2d4

Ikael
January 1st, 2017, 10:03
You could check CoreRPG 's StringManager.isDiceString function for hints how to include fudge dice

Bidmaron
January 1st, 2017, 13:42
The problem with my solution is that it would match 'ad6.' This is not a valid die specification (and I don't know why you would use that, but it probably shouldn't be thought of as a die). As for your recommendation, the official FG die string does not have a capital 'D' as permissible. It must be lower case. It turns out our glorious SW has already given us what we wanted in the StringManager. It is StringManager.findDiceMathExpression, which returns nil or the matched die expression.