title: PHP: Regex for exact match
body: <p>Here is the regex I currently have (which kind of works):</p>
  
  <pre><code>$regex = '/[\w ]{7,30}/';
  </code></pre>
  
  <p>My revision looks like what I want, but it does not work at all:</p>
  
  <pre><code>$regex = '^[\w ]{7,30}$';
  </code></pre>
  
  <p>Here is how I am using the regex:</p>
  
  <pre><code>public function isValid( $value )
  {
  
      $regex = '/^[\w ]{7,30}$/';
      return preg_match( $regex, $value ) ? true : false;
  
  }
  </code></pre>
  
  <p>I am trying to match the following:</p>
  
  <ul>
  <li>Any lower/upper case letter</li>
  <li>Any digit</li>
  <li>Can contain spaces</li>
  <li>Cannot contain line breaks or tab space</li>
  <li>Minimum of 7 characters</li>
  <li>Maximum of 30 characters</li>
  </ul>
  
  <p>Valid inputs:</p>
  
  <ul>
  <li>Testing</li>
  <li>Test ing</li>
  <li>Test123</li>
  <li>Test 123</li>
  <li>Test___</li>
  </ul>
  
  <p>Invalid inputs:</p>
  
  <ul>
  <li>Testing#</li>
  <li>Testin8+</li>
  <li>Tester1&amp;</li>
  </ul>
  
  <p>The first regex will match all valid inputs, as well as invalid (as long as the first four characters are valid, it doesn't care about the rest). The second regex matches nothing.</p>