title: Regular expression to extract numbers with either exactly 8 digits or with exactly 7 digits and discard if number have more digits
body: <p>I need to extract numbers with either 7 digits or 8 digits exactly from a column in my excel file and need to discard any numbers with more digits. </p>
  
  <p>For example assume these are some of the values</p>
  
  <p>2569123659                               | Extract nothing since > 8 digits</p>
  
  <p>2456789               |    2456789       | Extract 7 digit no</p>
  
  <p>15034891              |    15034891      | Extract 8 digit no</p>
  
  <p>2569123659 45785612   |    45785612      | Extract only 8 digit no</p>
  
  <p>2456789 2569123659    |    2456789       | Extract only 7 digit no</p>
  
  <p>ABC#2456789           |    2456789       | Extract only number</p>
  
  <p>ABC15034891DSC        |    15034891      | Extract only number</p>
  
  <p>ABC15034891###        |    15034891      | Extract only number</p>
  
  <p>The following are some of the expressions I tried</p>
  
  <blockquote>
    <p><strong>strPattern = "[0-9]{7}([0-9]{1})?"</strong></p>
  </blockquote>
  
  <p>Results: Wrong result</p>
  
  <p>2569123659            | 2456789</p>
  
  <p>2456789               | 15034891</p>
  
  <p>15034891              | 25691236</p>
  
  <p>2569123659 45785612   | 2456789</p>
  
  <p>2456789 2569123659    |  2456789</p>
  
  <p>ABC#2456789           |    2456789     </p>
  
  <p>ABC15034891DSC        |    15034891    </p>
  
  <p>ABC15034891###        |    15034891    </p>
  
  <blockquote>
    <p><strong>strPattern = "(\d{8}|\d{7})"</strong></p>
  </blockquote>
  
  <p>Results: Still Wrong result</p>
  
  <p>2569123659            | 25691236</p>
  
  <p>2456789               | 2456789</p>
  
  <p>15034891              | 15034891</p>
  
  <p>2569123659 45785612   | 25691236</p>
  
  <p>2456789 2569123659    | 2456789</p>
  
  <p>ABC#2456789           |    2456789     </p>
  
  <p>ABC15034891DSC        |    15034891    </p>
  
  <p>ABC15034891###        |    15034891  </p>
  
  <p>Help me rectify this reg expression so as to handle the scenario. I need to extract only the correct number. I need to avoid the extraction from another no with more digits.</p>
  
  <p>Any kind of help will be highly appreciated.</p>
  
  <p>Thanks in advance</p>
  
  <hr>
  
  <p>Based on answers provided here I also tried</p>
  
  <blockquote>
    <p><strong>strPattern = "\b(\d{7,8})\b"</strong></p>
  </blockquote>
  
  <p>Results: Still Wrong result</p>
  
  <p>2569123659            | </p>
  
  <p>2456789               | 2456789</p>
  
  <p>15034891              | 15034891</p>
  
  <p>2569123659 45785612   | 45785612</p>
  
  <p>2456789 2569123659    | 2456789</p>
  
  <p>ABC#2456789           |    </p>
  
  <p>ABC15034891DSC        |    </p>
  
  <p>ABC15034891###        |    </p>
  
  <p>Still not accurate</p>