PDA

View Full Version : Regular expression to match/extract "2d6" into 2,6,nil and "2d6+1" into 2,6,+1 ?



Varsuuk
July 28th, 2020, 06:51
I am having a mental block with regexes in Lua:

Trying to pickup the Num, DieType and Mod (inc the + or - in the result) using:


Debug.console(sHD:match("^(%d*)[dD](%d+)([+-]%d+)?"))


But this works if I feed it "2d6+2" but not if feed "2d6" for example
I've tried various mini changes nearly at random at this point as I am confused. I am pretty sure it is due to a comment in the Lua docs that says the modifiers only work on character-sets. And that there is a trick to doing what need later on. Haven't found that section in docs. Going back to it now. Figure ask in meanwhile.

damned
July 28th, 2020, 08:01
Try this: StringManager.isDiceString(sDice)

Varsuuk
July 28th, 2020, 08:11
OK - cool that script has a TON of regular expressions to look at for maybe tips (when not 3AM for me)

But the actual SCRIPT "isDiceString" won't do it for me.

What I am trying to do is extract the portions of the dice string itself. Now, I realized as I went down my stubborn path that I am fine stopping at just #d# part and don't technically need to capture the +/-# mod at end. But... it's something I don't understand how to do so I will keep investigating.

Bat damn, if I thought about WHY I was making this parse method (which is general) I would have realized don't need it to be general atm (but would be nice) :)


So, still - maybe StringManager will give me a tip on why my capture fails (if I add the capture of the +- at end, it HAS to have it or fails and ALL ThREE captures get nil instead of rightly just the third one.

damned
July 28th, 2020, 09:22
maybe this might work?


local sNumDice, sNumSides, sMod = string.match(sParams, "(%d+)[dD](%d+)(^%s-)");

damned
July 28th, 2020, 09:23
Oh and you cant use + or - as they are reserved characters

Varsuuk
July 28th, 2020, 13:35
TOTALLY spaced on the [+-], probably because thought [] allows only for single character and would interpret that way - so I fixed that part with hope. After failed, added your suggestion (though was confused by the "^" in the middle - doesn't that indicate "from start of line"?)

Runtime Notice: Reloading ruleset
Runtime Notice: s'aMajor: ' | { s'CoreRPG' = #4, s'MoreCore' = #1 } | s' aMinor: ' | { s'CoreRPG' = #0, s'MoreCore' = #58 }
Runtime Notice: s'onInit: registerResultHandler'
Runtime Notice: s'Replacing savingThrow() with Swords & Wizardry: White Box version.'
Runtime Notice: s'Calling onValueChanged() for HD'
Runtime Notice: s'npc.id-00002.hd'
Runtime Notice: s'1d6'
Runtime Notice: s'savingThrow(1d6)'
Runtime Notice: s'--------------'
Runtime Notice: nil
Runtime Notice: nil
Runtime Notice: nil
Runtime Notice: s'##############'



function parseHDInfo(sHD)

--return sHD:match("(^%d*)[dD]+(%d+)([+-]%d+)?*")
--return sHD:match("(^%d*)d(%d+)")
Debug.console("--------------")
sHD = "2d6"
Debug.console(string.match(sHD, "(%d+)[dD](%d+)(^%s-)"))
Debug.console(sHD:match("^(%d*)[dD](%d+)([%+%-]%d+)?"))
Debug.console(sHD:match("^(%d*)[dD](%d+)([%+%-]%d+)"))
Debug.console("##############")

damned
July 28th, 2020, 14:24
Im hoping that this
(^%s-)
means
not space, zero or more characters

damned
July 28th, 2020, 14:55
Here you go - this works in my testing

local foo = "2d6"
local sNumDice, sNumSides, sMod1 = string.match(foo, "(%d+)[dD](%d+)(.*)")
print(sNumDice)
print(sNumSides)
print(sMod1)
local foo = "2d6+1"
local sNumDice, sNumSides, sMod1 = string.match(foo, "(%d+)[dD](%d+)(.*)")
print(sNumDice)
print(sNumSides)
print(sMod1)



2
6

2
6
+1

celestian
July 28th, 2020, 17:16
Your best bet is to have 2 checks, one with 1d6 and 1d6+3. In actual regex you could do something like "^(%d+)[dD](%d+)([%+%-]%d+)?$" and it would match 1d6 and 1d6+3 but Lua doesn't seem to support ()?.

So using "^(%d+)[dD](%d+)([%+%-]%d+)?$" and "^(%d+)[dD](%d+)$" would catch both.

Varsuuk
July 28th, 2020, 22:40
Cool guys gonna check these out later tonight when things settle. Been on road most of day between appointment for son’s ambulatory surgery setup for Thurs And my OT (frozen shoulder) (he is “ok”, he had a bike accident where his nose was fractured in several places and twisted a bit. - they need to put him out to do the... adjustments) Tomorrow is covid test pre procedure and I gotta fit in my day job in between all this. Good thing I sleep little ...

Varsuuk
July 28th, 2020, 22:44
Your best bet is to have 2 checks, one with 1d6 and 1d6+3. In actual regex you could do something like "^(%d+)[dD](%d+)([%+%-]%d+)?$" and it would match 1d6 and 1d6+3 but Lua doesn't seem to support ()?.
So using "^(%d+)[dD](%d+)([%+%-]%d+)?$" and "^(%d+)[dD](%d+)$" would catch both.


Glad I mostly figured the right Regex out. I haven’t done much with them. Was looking at the lua book for format and saw the comment about not being able to apply modifiers to non char sets. But implied there were “advanced techniques” further on (which I did not detect/see/understand) :)


I’ll try your thing Damned! If it doesn’t do what I need, I will do it in two parts

Thanks so much for the helping hand

damned
July 28th, 2020, 23:10
the one i posted above works fine in my testing
string.match(foo, "(%d+)[dD](%d+)(.*)")

damned
July 28th, 2020, 23:58
Cool guys gonna check these out later tonight when things settle. Been on road most of day between appointment for son’s ambulatory surgery setup for Thurs And my OT (frozen shoulder) (he is “ok”, he had a bike accident where his nose was fractured in several places and twisted a bit. - they need to put him out to do the... adjustments) Tomorrow is covid test pre procedure and I gotta fit in my day job in between all this. Good thing I sleep little ...

Good luck with all the medical drama!
My wife's frozen shoulder is hopefully almost better - after more than 18 months.

Varsuuk
July 29th, 2020, 02:17
And I can verify that Damed's version does the job for me. I appreciate it.

I am going to use that one, however the failing it has is that it does not "validate" the last segment. 2d6+Fred would parse as would 2d6ILikePez with (my guess) "+Fred" and "ILikePez" for the "mod" section.

I am fine with throwing an error in this case for now. Later I may get jiggier with it.

damned
July 29th, 2020, 02:22
Thats true - but neither is there any validation on the 2d6 part...
I usually run the validation in an earlier line and spit out a warning if its invalid

Varsuuk
July 29th, 2020, 03:03
Hmmm ... seems like missed something brb..