title: Regex for fixed width field
body: <p>I need to match a fixed width field on a file layout with a regular expression. The field is numeric/integer, always have four characters and is included in the range of 0..1331. When the number is smaller than 1000, the string is filled with left zeros. So all these examples are valid:</p>
  
  <ul>
  <li>0000</li>
  <li>0001</li>
  <li>0010</li>
  <li>1000</li>
  <li>1331</li>
  </ul>
  
  <p>But the following must be <strong>not</strong> accepted:</p>
  
  <ul>
  <li>1</li>
  <li>01</li>
  <li>10</li>
  <li>100</li>
  <li>4759</li>
  </ul>
  
  <p>It would be nice if I could enforce this restriction only with regex. After playing a bit, I yielded the expression <code>\0*[0-1331]\</code>. The problem is that it does not restrict the size to four characters. Of course I could do <code>\000[0-9]|00[10-99]|0[100-999]|[1000-1331]\</code> but I refuse to use something so nasty. Can anyone think of a better way?</p>