Skip to content

Instantly share code, notes, and snippets.

@lodestone
Created May 6, 2014 04:35
Show Gist options
  • Save lodestone/11553367 to your computer and use it in GitHub Desktop.
Save lodestone/11553367 to your computer and use it in GitHub Desktop.
def parse(dices)
%r{^
(?<top_or_bottom>t|b)? # Flag for finding the top or bottom dice
(?<tb_number>\d)? # Number of top or bottom dice
\[(?<dice>.*)\] # The main dice string, to be parsed later
(?<explode>\*)? # Reroll (explode) die rolls? (i.e. 6 on 1d6)
(?<plus>(\+|\-)\d+)? # Additional points to add to roll (or subtract)
\=? # = Target Number or range follows...
(?<target>.*)?$ # The target number (single 4, range 4..6, above 4+, below 4-)
}x =~ dices
die_set = eval "[" + string_to_dice_array(dice) + "]"
dice_pool({set: die_set, top: tb_number.to_i, plus: plus.to_i})
end
@darrencauthon
Copy link

That is one readable regex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment