title: D&D-style compound dice expression regex
body: <p>I'm working on a project in Java that involves a user entering a dice expression. I'm using a regex to validate the input to ensure it's a valid D&amp;D style dice expression. What I mean by that is that expressions like these:</p>
  
  <pre><code>d20
  2D12+ 4 - 2
  12d4-2d6+7 + d8
  7 + 1d12
  </code></pre>
  
  <p>Would all be valid. Whereas expressions like these:</p>
  
  <pre><code>-2d6+4
  -d8 - 5
  12
  2d0 + 5
  1d6 + 0d2 - 1
  0d6
  </code></pre>
  
  <p>Would all be invalid.
  So far, the closest regex I've managed to make is:</p>
  
  <pre><code>^((((0+\d+|[1-9]\d*|)(d|D)(0+\d+|[1-9]\d*))|(\d+))((\ *(\+|\-)\ *)(?=(((0+\d+|[1-9]\d*|)(d|D)(0+\d+|[1-9]\d*))|(\d+))))?)+$
  </code></pre>
  
  <p>However, this regex still matches expressions like "0d6". Any assistance in creating a regex for this would be much appreciated.</p>