Detect Complex Dice Formats

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Detect Complex Dice Formats



I am currently working on a program that scans a page and highlights dice rolls and then lets you click to rolls those dice.



The program mostly works as expected, for example if the page contains stuff like 1d6, 1d6 + 3, d20, or 1d6 x 100 it picks up and rolls it. However I would like the ability to detect more complicated roles for example 1d20 + 4 1d12 + 12 and then parse that out into its individual roles. I would like to avoid detecting things like 1d20 + 4 the 1d12 + 12, basically, avoid things that have words instead of spaces in the dice roll.


1d6


1d6 + 3


d20


1d6 x 100


1d20 + 4 1d12 + 12


1d20 + 4 the 1d12 + 12



My script uses regex to match patterns on the page and highlight them. I am not sure if this is something I could add to my regex since this is really been my project for learning regex. Here is the current regex I am using



([+−-]d+)|(([1-9]d*)?d([1-9]d*)s*([+-−]s*d+)?([,]s*d+)?)


([+−-]d+)|(([1-9]d*)?d([1-9]d*)s*([+-−]s*d+)?([,]s*d+)?)



And a sample on https://regexr.com/3tl1o





Could you provide some docs about this syntax? How can I know what's should be matched or not? Is this it: en.wikipedia.org/wiki/Dice_notation?
– sp00m
Aug 8 at 12:33






Also, in your regexr, what's matched that shouldn't be (or vice versa)?
– sp00m
Aug 8 at 12:41





1 Answer
1



I think this is what you want



((?:[+-]?[1-9]d*)?d[1-9]d*(?:s+[-+x]s+[1-9]d*)?(?:s+(?:[+-]?[1-9]d*)?d[1-9]d*(?:s+[+-x]s+[1-9]d*))*)


((?:[+-]?[1-9]d*)?d[1-9]d*(?:s+[-+x]s+[1-9]d*)?(?:s+(?:[+-]?[1-9]d*)?d[1-9]d*(?:s+[+-x]s+[1-9]d*))*)



See https://regex101.com/r/Ap0HZw/1 for the explination






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard