title: Parse period of time in Java
body: <p>I want to be able to parse period of time (days, hours, minutes) + optional string so the input looks like this: <code>&lt;time&gt;(white_spaces)&lt;optional_string&gt;</code>. As i know regex is the right tool for such things so i came up with such expression: </p>
  
  <pre><code>Pattern.compile("((?&lt;days&gt;\\d+)d)?((?&lt;hours&gt;\\d+)h)?((?&lt;minutes&gt;\\d+)m)?\\s+?(?&lt;reason&gt;.+)?");
  </code></pre>
  
  <p>Basically it works as expected however in this expression all times groups (days, hours, minutes) are optional and i want the input to atleast contain minutes group. However if hours or days are specified, minutes are not required. Also, all combinations of time groups (d+h, h+m, d+m, d+h+m) are possible. So how can i correct my expression? Or maybe there is other way to parse period of time? </p>
  
  <p>EDIT:
  examples of inputs:</p>
  
  <p><code>12h64m</code> - correct</p>
  
  <p><code>12d43m dsd</code> - correct </p>
  
  <p><code></code> - empty string - not correct</p>
  
  <p><code>12m</code> - correct </p>
  
  <p><code>12d32h43m</code> - correct</p>
  
  <p><code>sdsds</code> - not correct - no "time group specified"</p>