DICE PACKS BUNDLE
Page 1 of 2 12 Last
  1. #1
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075

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

    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.

  2. #2

  3. #3
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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.

  4. #4

  5. #5

  6. #6
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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'##############'

    Code:
    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("##############")
    Last edited by Varsuuk; July 28th, 2020 at 13:49.

  7. #7

  8. #8
    damned's Avatar
    Join Date
    Mar 2011
    Location
    Australia
    Posts
    26,674
    Blog Entries
    1
    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

  9. #9
    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.
    ---
    Fantasy Grounds AD&D Reference Bundle, AD&D Adventure Bundle 1, AD&D Adventure Bundle 2
    Documentation for AD&D 2E ruleset.
    Custom Maps (I2, S4, T1-4, Barrowmaze,Lost City of Barakus)
    Note: Please do not message me directly on this site, post in the forums or ping me in FG's discord.

  10. #10
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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 ...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
STAR TREK 2d20

Log in

Log in