title: Regex: Match unbalanced and balanced strings in two capture groups
body: <p>I am pretty decent with <code>regex</code> matching, but this below has me stumped this Friday afternoon. I have the below 3 variants of a string in a "date" format that I need to send to <code>MySQL</code> as a range to get the matching results in between. As a background the data comes from a <code>JSON</code> source I have no control over, and is processed by <code>Perl</code> before being passed to <code>MySQL</code>. Everything works great except for the last variant. </p>
  
  <p><strong>Variants to Match</strong></p>
  
  <pre><code>2014-04-01~2014-04-16
  ~2014-04-16
  2014-04-01~
  </code></pre>
  
  <p>The first one is a range. The second one is from the beginning of time to the date, and the last one is from the date to the end of time.</p>
  
  <p><strong>Current Regex</strong></p>
  
  <pre><code>/(\d{4}-\d{2}-\d{2})~|~(\d{4}-\d{2}-\d{2})/g;
  </code></pre>
  
  <p><strong><a href="http://regex101.com/r/vD0kT5" rel="nofollow">Regex101</a></strong></p>
  
  <p>Now I have been through a ton of <code>Regex</code> combinations and the one listed is merely the last one I was working with. </p>
  
  <p>The issue is that I need to specify the dates as <code>from</code> and <code>to</code>. This is fine when I have both dates as I have two capture groups, and okay with only the start time as it is automatically the first capture group, but where I am having difficulties is when only the "from the beginning of time to the date" is used as it is placed in the first capture group and as shown below I would LIKE it to be in the second capture group.</p>
  
  <p><strong>Example Usage in Perl</strong></p>
  
  <pre><code>my $date = '~2014-04-01';
  
  my ($f,$t) = $date =~ /(\d{4}-\d{2}-\d{2})/gm;
  print "FROM: " . $f . "\n";
  print "TO: " . $t . "\n";
  </code></pre>
  
  <p>I don't want to use multiple <code>regex</code> patterns in the <code>perl</code> and would like to limit the <code>perl</code> to only the above with the <code>regex</code> doing the heavy lifting.</p>
  
  <p>Can anyone provide a solution for the <code>regex</code> in this case?</p>