title: Matching As followed by the same number of Bs in egrep
body: <p>Suppose I want to match a pattern with the exact same number of characters A and B such that there are exactly n A's followed by exactly n B's.
  For example, the following strings can be matched.</p>
  
  <ol>
  <li>AB </li>
  <li>AABB</li>
  <li>AAABBB</li>
  </ol>
  
  <p>On the other hand, these strings cannot be matched</p>
  
  <ol>
  <li>BA </li>
  <li>AAABB</li>
  <li>AABBB</li>
  <li>ABAB</li>
  </ol>
  
  <p>To approach the problem, I am thinking about the repetition counts, so my attempt looks like this</p>
  
  <pre><code>egrep 'A{n}B{n}'
  </code></pre>
  
  <p>of course, however, the repetition count n inside the curly bracket cannot be defined implicitly.</p>
  
  <p>While I know how to write programs to match it, I am testing this on Mac terminal,  hence I am trying to exploit any possible features of egrep to write the one sentence pattern.</p>
  
  <p>So could anyone please help me solve this problem and any help will be appreciated.</p>