title: Using Regex to validate a variable length string
body: <p>I'm trying to validate a form field in Java using Regex, which can have 5 different format possibilities. I'm struggling to get this one working.</p>
  
  <p>The string to be checked will be between 4-6 alphanumeric characters.</p>
  
  <p>If it's 4 characters, it must be all numbers.</p>
  
  <pre><code>^\\d{4}$
  </code></pre>
  
  <p>If it's 5 characters, it can be all numbers, first position letter with 4 following numbers, or first 3 positions letters followed with 2 numbers.</p>
  
  <pre><code> ^\\d{5}$
   ^[a-zA-Z]\\d{4}$
   ^[a-zA-Z]{3}\\d{2}$
  </code></pre>
  
  <p>And if it's 6 characters, it will be first position letter, 4 numbers, and last another letter. </p>
  
  <pre><code>^[a-zA-Z]\\d{4}[a-zA-Z]$
  </code></pre>
  
  <p>I just can't seem to piece it all together though. </p>