title: Regular expression for 7 digits followed by an optional 3 letters
body: <p>I'm new to regular expressions, and I'm trying to validate receipt numbers in our database with a regular expression.</p>
  
  <p>Our receipts can come in the following formats:</p>
  
  <ul>
  <li>0123456 (Manditory seven digits, no more, no less)</li>
  <li>0126456a (Manditory seven digits with one letter a-z)</li>
  <li>0126456ab (Manditory seven digits with two letters a-z)</li>
  <li>0126456abc (Manditory seven digits with three letters a-z)</li>
  </ul>
  
  <p>I've tried using a bunch of different regex combinations, but none seem to work. Right now I have:</p>
  
  <pre><code>(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?
  </code></pre>
  
  <p>But this is allowing for more than seven digits, and is allowing more than 3 letters.</p>
  
  <p>Here is my VBA function within Access 2010 that will validate the expression:</p>
  
  <pre><code>Function ValidateReceiptNumber(ByVal sReceipt As String) As Boolean
  
      If (Len(sReceipt) = 0) Then
          ValidateReceiptNumber = False
          Exit Function
      End If
  
      Dim oRegularExpression     As RegExp
  
  '   Sets the regular expression object
      Set oRegularExpression = New RegExp
  
      With oRegularExpression
  '   Sets the regular expression pattern
          .Pattern = "(\d)(\d)(\d)(\d)(\d)(\d)(\d)([a-z])?([a-z])?([a-z])?"
  
  '   Ignores case
          .IgnoreCase = True
  
  '       Test Receipt string
          ValidateReceiptNumber = .Test(sReceipt)
  
      End With
  End Function
  </code></pre>