title: PHP regular expression for given condition
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5112520/php-regex-extract-phone-number-from-text-html">php regex , extract phone number from text/html</a>  </p>
  </blockquote>
  
  
  
  <p>I have following requirement. There is a phone number field.</p>
  
  <ul>
  <li>Its length should be between 8 and 10</li>
  <li>It can contain only _ and white spaces</li>
  <li>First and last elements should be digits</li>
  <li>_ and white spaces should be in between 2 digits</li>
  </ul>
  
  <p>Can I write a regular expression to validate the above things using php?</p>
  
  <p>Example for valid entry: 231_901 347
  Example for invalid entry: _123 567, 345__123</p>
  
  <p>Also if possible, can you please write down the preg_match expression.</p>
  
  <p>Here is the code which I tried:</p>
  
  <pre><code>$subject = "_012_345 69";
  $pattern = '/[\d]{1}.[\d\s_]{5,10}.[\d]{1}$/';
  $matches = preg_match($pattern, $subject);
  echo($matches);
  </code></pre>
  
  <p>It should return 0 but it returns 1.</p>