title: Use regex to match a^xb^x, where x is the number of times a,b appear
body: <p>To clarify, I want to know how to use a regex to match:</p>
  
  <pre><code>ab
  aabb
  aaabbb 
  ...
  </code></pre>
  
  <p>I just found out that this works in <code>Perl</code>: 
  </p>
  
  <pre><code>if ($exp =~ /^(a(?1)?b)$/)
  </code></pre>
  
  <p>To understand this, look at the string as though it grows from the outside-in, not left-right:</p>
  
  <pre><code>ab
  a(ab)b
  aa(ab)bb
  </code></pre>
  
  <p><code>(?1)</code> is a reference to the outer set of parentheses.  We need the <code>?</code> afterwards for the last case (going from outside in), nothing is left and <code>?</code> means 0 or 1 of the preceding expression (so it essentially acts as our base case).  </p>
  
  <p>My questions is:  What is the equivalent of <code>(?1)</code> in Java?  </p>